Programming in C QUESTION BANK
Programming in C QUESTION BANK
scanf(“%f%f%f”,&a,&b,&c);
Page
input:
enter a number for multiplication table: 5
output:
5x1= 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
long factorial(int);
Page
int num;
long f;
if (num < 0)
printf("Negative numbers are not allowed.\n");
else
{
f = factorial(num);
printf("factorial of %ld is %ld\n", num, f);
}
getch();
}
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
input
Enter a number n= 5
output
Factorial of 5 is 120.
4. Write a C program to Create a single dimensional array of numbers)) and display the
contents. and find the biggest number from this array.
Ans: #include <stdio.h>
#include<conio.h>
void main()
{
int array[100], maximum, size, c, location = 1;
clrscr();
printf("Enter the number of elements in an array:");
scanf("%d", &size);
printf("\nMaximum element is present at location number %d and it's value is %d.\n", location,
maximum);
getch();
}
Input
Enter the number of elements in an array:5
Enter 5 integers
22
34
53
45
26
output
given array
22
34
53
45
26
Maximum element is present at location number 3 and it's value is 53.
5. Write a C program to Create a single dimensional array of numbers and display the contents and
Arrange this single dimensional array of numbers into ascending order.
Ans:
#include <stdio.h> for (c = 0 ; c < n - 1 ; c++)
#include<conio.h> {
for (d = 0 ; d < n - c - 1; d++)
void main() {
{ if (array[d] > array[d+1]) /* For decreasing
int array[100], n, c, d, order use < */
swap; clrscr(); {
swap = array[d];
printf("Enter number of elements:"); array[d] = array[d+1];
scanf("%d", &n); array[d+1] = swap;
}
printf("Enter %d integers\n", n); }
}
for (c = 0; c < n; c++)
scanf("%d", &array[c]); printf("Sorted list in ascending order:\n");
%d",array[c]); getch();
Page
6. Write a C program to Create a single dimensional array of numbers and display the contents and
Arrange this single dimensional array of numbers into descending order.
Ans:
#include <stdio.h>
#include<conio.h>
void main()
{
int array[100], n, c, d, swap;
clrscr();
getch();
}
Input:
Enter number of elements: 6
void main()
{
char arr[100];
getch();
}
input
Enter a string to to find length of it: venkata ramana
output
The string length = 14
void main()
{
char arr[100];
strrev(arr);
getch( );
Page
#include<stdio.h>
#include<conio.h>
void main()
{
int ar,ac,br,bc, c, d, e, a[10][10], b[10][10], mul[10][10];
clrscr();
printf("Enter the number of rows in matrix-A: ");
scanf("%d", &ar);
printf("Enter the number of columns in matrix-A: ");
scanf("%d", &ac);
printf("\n");
}
8
printf("\n");
}
printf(" A x B :-\n");
printf("\n");
}
goto endpara;
lastpara:printf("\n columns in matrix -A is not equal to rows in matrix -B, hence can not
multiply");
endpara:
getch();
}
input
Enter the number of rows in matrix-A:2
Enter the number of columns in matrix-A: 2
Enter the number of rows in matrix-B:2
Enter the number of columns in matrix-B: 2
Enter 4 elements of first matrix-A
1
2
3
4
output
matrix-A is
1 2
3 4
matrix-B is
5 6
7 8
A x B :-
19 22
43 50
9
Page
{ int Fibonacci(int);
int n, i = 0, c;
printf("Enter the number of terms:\n");
scanf("%d",&n);
getch();
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
Input
Enter the number of terms: 10
Output
First 10 terms of Fibonacci series are :-
0
1
1
2
3
5
8
13
21
34
10
Page
input:
output:
a + b = 18
a-b=8
a * b = 65
a/b=2
a modulus b = 3
}
Page
input:
input:
output:
Page
getch();
}
input
Enter an integer: 5
Output
5 is an Odd number
14. Write a C program to
(a) Write a C program to find whether given number is Prime number or not
Ans:
/* A prime no is positive numbers that have just two factors that is 1 & itself */
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,flag=0;
clrscr();
printf("\n Enter any number : ");
scanf("%d",&n);
for(i=2;i<=n/2;i++)
{
if(n%i= =0)
{
flag=1;
break;
}
}
if(flag= =1)
14
else
if(n%i==0)
{
c=c+1;
}
}
if (c==2)
printf("%d is a prime",n);
else
printf("%d is not a prime",n);
getch();
}
input
enter the number to be checked: 5
output
5 is a prime
Input
enter the number to be checked: 6
output
6 is not a prime
(b). Write a function and call a function to perform add, subtract and multiply two numbers.
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int add(int x , int y);
15
Input
enter two integers 3 5
Output
3 + 5 is 8
3 - 5 is -2
3 * 5 is 15
15. (a). Write a C program to Create a structure( struct) by name “book” containing book no., book
name, author and cost as members. Create book1 and book2 as copies of this structure and display
the values for two books. Display the total cost of the books.
Ans:
#include <stdio.h>
#include <conio.h>
void main()
{
struct book
{ int bookno;
char bookname[20];
char author[20];
int cost;
} book1 , book2;
int total;
16
clrscr();
Page
getch();
}
Input
Enter Book No : 9440
Enter Book Name : MS-Office
Enter author name : Sathish
Enter Book cost : 350
output
Book No : 9440
Book Name : MS-Office
author name : Sathish
Book cost : 350
17
Page
Book No : 5595
(b).Create a structure by name “employee” with necessary data members and create an array of
5 employees and display the values.
Ans:
#include <stdio.h>
#include <conio.h>
void main()
{
struct employee
{ int empidno;
char empname[20];
char designation[20];
int salary;
} emp[5];
int i,n;
clrscr();
printf("\n how many employees are to be entered");
scanf("%d",&n);
for (i=0;i<n;++i){
for (i=0;i<n;++i){
getch();
Page
Output:
employee list
Employee IDNo : 101
Employee Name : YOUNUS
Designation : JUDGE
Salary : 45000
16 (a) Use file operation functions to read, write and append the data to and from files Use file
operation functions
Ans:
a) Read a file.
#include<stdio.h>
#include<stdlib.h>
void main()
{
char ch, file_name[25];
FILE *fp;
if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
fclose(fp);
getch();
19
}
Page
#include <stdio.h>
#include <stdlib.h>
void main()
{
char ch, source_file[20], target_file[20];
FILE *source, *target;
fclose(source);
fclose(target);
getch();
}
(b). Write a program to create a simple text file and write and read data from using functions like
fopen() etc.
Ans:
a) write a file
20
Type file.txt
Dharmaraja
Bheema
Arjuna
b) append a file
/*append a file(add some names to a file)*/
#include <stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
int i,n;
char sname[20];
clrscr();
fp = fopen("file.txt","a");
/*Create a file and add text*/
printf("\n how many names you want to add(append) to a file:");
21
scanf("%d",&n);
for(i=1;i<=n;++i){
Page
input
how many names you want to add(append) to a file: 2
enter a name : Nakula
enter a name : Sahadeva
Output
Output is stored in the file named file.txt.
so to get the output
give the following command at the location where the file is stored
Type file.txt
Dharmaraja
Bheema
Arjuna
Nakula
Sahadeva
c) Read a file
/*reads a file */
#include <stdio.h>
#include<conio.h>
void main( )
{
FILE *fp;
char c;
clrscr();
fp = fopen("file.txt", "r");
if (fp == NULL)
printf("File doesn't exist\n");
else
do {
c = getc(fp); /* get one character from the file */
putchar(c);} /* display it on the monitor */
while(c != EOF);
fclose(fp);
getch();
}
Output
Dharmaraja
Bheema
22
Arjuna
Nakula
Page
#include <stdio.h>
int main()
{
int i, num, sum = 0;
return 0;
Page
String concatenation
/*string concatenation */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[100], b[100];
strcat(a,b);
getch();
24
}
input
Page
string copy
/*string copy */
#include<stdio.h>
#include<string.h>
void main()
{
char source[] = "abc";
char destination[]="xyz";
strcpy(destination, source);
printf("\n After copy\n");
}
output
Before copy
Source string: abc
Destination string: xyz
After copy
Source string: abc
Destination string: abc
#include<conio.h>
void main ( )
Page
if(year%4 == 0)
{
Page
if( year%100 == 0)
return 0;
}
Output 1 Output 2
Enter a year: 1900 Enter a year: 2012
1900 is not a leap 2012 is a leap year
year.
Ans: A flow chart is a step by step diagrammatic representation of the logic paths to solve a given
problem. Or a flow chart is visual or graphical representation of an algorithm. It is methods to be used
to solve a given problem and help a great deal to analyze the problem and plan its solution in a
systematic and orderly manner. A flowchart when translated in to a proper computer language, results
in a complete program.
27
Page
The if-else statements is used to execute the either of the two statements depending upon
the value of the exp (expression) or condition. The general form is
if(<exp>)
{
Statement-1;
Statement -2;
………….. “ SET-I”
……………
Statement- n;
}
else
{
Statement1;
Statement 2;
………….. “ SET-II”
……………
Statement n;
}
SET - I Statements will be executed if the exp is true.
SET – II Statements will be executed if the exp is false.
Example:
if ( a> b )
printf (“a is greater than b”);
else
printf (“a is not greater than b”);
28
Page
Switch case is used to select a single statement from set of several alternatives statements. It is
made with an option of choices from set of statements. The variable in the switch executes only a
particular statement whenever it satisfies the case constant.
Syntax:
switch(exp)
{
case constant-1: statement 1;
break;
case constant-2: statement 2;
break;
case constant-n:
statement n;
break;
default
: statement;
}
Break: The break statement are used inside each case of the switch, causes an intermediate exit
from the switch statement.
Default: When any case is not matched then this statement will be executed.
Ans: A function can be defined as a subprogram which is meant for doing a specific task. In a C
program, Or function is a self contained block of statement that specifies one or more actions to be
performed for the large program, a function definition will have name and parentheses pair
Ans: Array is a collection of similar data elements. Which can be referring by a single name. The
array variables values will be stored in sequence (sequential memory allocation). An array is also
known as a subscripted variable. Basically arrays can divide in to
1. One Dimensional Array
2. Two Dimensional Array
27. Write the differences between Iteration and Recursion.
Ans: Recursion can be defines as the process of a function by which it can call itself. The function
which calls itself again and again either directly or indirectly based on condition is known as recursive
function.
The iteration means repetition of process until the condition becomes false. For example – when we
use loop (for, while etc.) in our programs. It involves four steps, initialization , condition, execution
and updation. The iteration is applied to the set of instructions which we want to get repeatedly
executed.
Ans: There are 4 basic operations that can be performed on any files in C programming language.
They are,
Ans: A group of one or more variables of different data types organized together under a single
name is called Structure. a structure can be viewed as a heterogeneous (dissimilar) user-defined
data type. When a structure is defines the entire group s referenced through the structure name.
The individual components present in the structure are called structure members and those can be
accessed and processed separately.
Syntax:
struct <struct name>
{
data type variable name;
data type variable name;
} one or more structure variables;
Example: struct employee
30
{
Page
int eno;
Syntax:
union name {
data type member-1;
data type member-2;
data type member-3;
………………….
…………………
data type member-n;
};
Example :
union value
{
int no;
float sal;
char sex;
};
31
Page