C Programming Question Bank
C Programming Question Bank
NNCE I / 02 C / QB
CS3251- PROGRAMMING IN C
I YEAR / II SEMESTER
PREPARED BY
VERIFIED BY
1
Dr.NNCE I / 02 C / QB
PART- A 2 Marks
1. Write down the steps involved in writing a program to solve a problem.
To design a program, a programmer must determine three basic steps:
a. The instruction to be performed.
b. The sequence in which those instructions are to be performed.
c. The data required to perform those instructions.
3
Dr.NNCE I / 02 C / QB
4
Dr.NNCE I / 02 C / QB
18. What is identifier? Give any two examples for an identifier. (Jan 2009)
Identifiers are the names given to variable program elements such as variables, functions and
arrays. Ex. STDNAME, sub, TOT_MARKS,sub123
19. Define Operator. List out the types of Operator.
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. C language is rich in built-in operators and provides following type of operators
a. Arithmetic Operators d. Bitwise Operators
b. Relational Operators e. Assignment Operators
c. Logical Operators f. Special Operators
20. What is the difference between ++a and a++?
++a means do the increment before the operation (pre increment) a++ means do the increment
after the operation (post increment).
21. Give two examples for logical and relational expression. (Jan 2011)
Relational Expression Logical Expression
(a>b) if((a>b)&&(a>c))
(a==b) if((a>b)||(a>c))
22. What are the Bitwise Operators available in C? (Jan 2011)
It is used to manipulate the data at bit level. It operates only integers.
Operator Meaning
& Bitwise AND
! Bitwise OR
^ Bitwise XOR
<< Shift Left
>> Shift Right
~ One‟s complement
23. Write the following conditions using ternary operator. (Jan 2009)
4x+100 for x<40
Salary= 300 for x=40
Salary= x<40?4*x+100:x>40?4.5*x+150:300;
24. What is type casting?
Type casting is a way to convert a variable from one data type to another data type. For
example, if you want to store a long value into a simple integer then you can typecast long to
int. You can convert values from one type to another explicitly using the cast operator.
int x,y;
c = (float) x/y;
where x and y are defined as integers. Then the result of x/y is converted into float.
25. What do you meant by conditional or ternary operator? Give an example for Ternary
operator. (Nov 2014)
Conditional or ternary operator‟s returns one value if condition is true and returns another
5
Dr.NNCE I / 02 C / QB
value is condition is false. Using ?: reduce the number of line codes and improve the performance
of application.
Syntax: (Condition? true_value: false_value);
Ex: a<b ? printf("a is less") : printf("a is greater");
is allocated to that data types. sizeof(char);
//1
26. What is use of Sizeof() Operators in C?
sizeof() is used with the data types such as int, float, char… it simply return amount of memory is
allocated to that data types.
sizeof(int); //4
sizeof(float); //4
sizeof(double); //8
sizeof() is used with the expression, it returns size of the expression. int a
= 0;
double d = 10.21; printf("%d",
sizeof(a+d)); //8
27. Mention the various decisions making statement available in C.
a. if statement
b. if...else statement
c. nested if statements
d. switch statement
e. nested switch statements
6
Dr.NNCE I / 02 C / QB
b. The condition is checked at the starting b. The condition is checked at the end of
of the loop. the loop.
c. It is an entry controlled loop. c. It is an exit controlled loop.
31. Write a for loop statement to print numbers from 10 to 1. (Jan 2014)
#include <stdio.h>
Output:
void main(void)
{ 10 9 8 7 6 5 4 3 2 1
int count; // Display the numbers 10 to 1 Press any key to continue . . .
for(count = 10; count >= 1; count--)
printf("%d ", count);
}
32. What are the two types of I/O Statements in C?
There are two types of I/O Statements in C.
1. Formatted I/O Statements
2. Unformatted I/O Statements
33. Differentiate between formatted and unformatted functions.
Formatted I/O functions Unformatted I/O functions
a. Formatted data requires more space than a. Unformatted input/output is usually the
unformatted to represent the same most compact way to store data.
information. b. Unformatted input/output is the least
b. Formatted input/output is very portable. portable form of input/output.
c. It is a simple process to move formatted data c. Unformatted data files can only be moved
files to various computers, even computers easily to and from computers that share
running different operating systems, as long the same internal data representation.
as they all use the ASCII
character set.
34. List the various input and output statements in C. (May 2015)
The various input and output statements in C are
7
Dr.NNCE I / 02 C / QB
36. What are the pre-processor directives? (Jan 2014, May 2014, 2015)
Preprocessor directives are the commands used in preprocessor and they begin with “#” symbol.
Before a C program is compiled in a compiler, source code is processed by a program called
preprocessor. This process is called preprocessing.
Macro Syntax: #define
This macro defines constant value and can be any of the basic data types.
Header file Syntax: #include <file_name>
inclusion The source code of the file “file_name” is included in the main program at
the specified place.
Conditional Syntax: #ifdef, #endif, #if, #else, #ifndef
compilation Set of commands are included or excluded in source program before
compilation with respect to the condition.
Unconditional Syntax: #undef, #pragma
compilation #undef is used to undefine a defined macro variable. #Pragma is used to call
a function before and after main function in a C program.
37. What is meant by storage Class?
A storage class is the one that defines the scope (visibility) and life time of variables and/or functions with
in a C program.
38. What are the storage classes available in C?
Storage classes are categorized in four types as,
Automatic Storage Class - auto
Register Storage Class - register
Static Storage Class - static
External Storage Class - extern
39. What is register storage in storage class?
Register is used to define local variables that should be stored in a register instead of RAM. This
means that the variable has a maximum size equal to the register size (usually one word) and can‟t
have the unary '&' operator applied to it (as it does not have a memory location). Ex. register int
a=200;
40. What is static storage class? (Nov 2014)
Static is the default storage class for global variables. The static storage class object will be
stored in the main memory.
Ex. static int Count=19;
41. Define Auto storage class in C.
Auto is the default storage class for all local variables. The auto storage class data object will be
stored in the main memory. These objects will have automatic (local) lifetime.
Ex. auto int Month;
42. Define Macro in C. What is the use of #define preprocessor? (Nov 2014)
A macro can be defined as the facility provided by the C preprocessor by which a token can be
replaced by the user defined sequence of characters. Macros are defined with the help of define
directive. Its format is: #define identifier replacement.
8
Dr.NNCE I / 02 C / QB
PART-B:
1. Explain in detail about the Structure of C Program.
STRUCTURE OF C PROGRAM
Documentation section
Preprocessor section
Definition section
9
Dr.NNCE I / 02 C / QB
10
Dr.NNCE I / 02 C / QB
The programmers can write the statement anywhere between the two braces.
We can write one or more statements in one line by separating each statement with semicolon (;)
The opening and closing braces should be balance
Automatic Variable:
The variables without any specification are considered as automatic variables.
They are defined inside a function.
They are called as automatic because their memory space is automatically allocated
as the variable is declared.
These variable has only temporary memory space .
after execution all automatic variables are disposed. It can not be accessed directly.
Syntax:
storage_class_type data_type var1,var2,…varn;
Example:
auto int a,b;
auto float x,y;
auto char sex
Example Program: Output:
#include<stdio.h> c=340
#include<conio.h> c=458
void main() c=340
{
int c=340;
clrscr();
printf(“c=
%d”,c);
{
int c=458;
printf(“c=
%d”,c);
}
printf(“c=
%d”,c);
getch();
}
11
Dr.NNCE I / 02 C / QB
Static Variable:
Static variables are the variables for which contents of variables available
throughout the program.
It may be an internal or external type, depending upon where it is declared.
If declared outside of the body , it will be a static or global.
If it is declared inside the body or block it will be auto variable.
These are permanent with in the function.
Syntax:
storage_class_type data_type var1,var2,…varn;
Example:
static int a,b;
static float x,y;
static char sex;
12
Dr.NNCE I / 02 C / QB
Example:
extern int a,b;
extern float x,y;
extern char sex;
Example Program: Output
#include<stdio.h> in fun1() d=124
#include<conio.h> in fun2() d=510
int d=510; in main() d=510
void main()
{
clrscr();
fun1();
fun2();
printf(“in main() d=
%d”,d); getch();
}
fun1()
{
int d=124;
printf(“in fun1() d=%d”,d);
}
fun2()
{
printf(“in fun2() d=%d”,d);
}
Register Variable
It is a special storage area in central processing unit.
The arithmetic and logical operations are carried out with in these register.
Register access is faster than memory access.
The values of register variable stored with in the registers of computer cpu rather
than in memory.
Syntax:
Storage_class_type data_type var1,var2,…varn;
Example:
register int a,b;
13
Dr.NNCE I / 02 C / QB
2;i++)
{
printf(“value of i is:%d”,i);
}
getch();
}
3. Explain various types of operators in C language with suitable example.
OPERATORS
An operator is a symbol that specifies an operation to be performed on the operands.
The data items that operators act upon are called operands.
Operators that require two operands are called binary operators.
Operators that act upon only one operand is called unary operators.
Example: a+b
Here a, b -> Operator + -> Operand
Types of operators
a) Arithmetic Operators
b) Relational Operators
c) Logical Operators
d) Assignment Operators
e) Increment/ Decrement Operators
f) Conditional Operators(Ternary Operator)
g) Bitwise Operators
h) Special Operators
ARITHMETIC OPERATOR
14
Dr.NNCE I / 02 C / QB
a+b 10.0
a-b 3.0
#include
<stdio.h> int
main()
{
int a = 9,b = 4, c; output
c = a+b; a+b=13
printf("a+b = %d \n",c); a-b=5
c = a-b; a*b=36
printf("a-b = %d \n",c); a/b=2
c = a*b; Remainder when a divided
by b=1 printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \
n",c); c = a%b;
printf("Remainder when a divided by b = %d \
n",c); return 0;
}
RELATIONAL OPERATORS:
Used to compare two or more operands.
Operato Meaning Exampl Return
r e Values
== is equal to 2==3 0
15
Dr.NNCE I / 02 C / QB
Syntax:
#include<stdio.h>
#include<conio.h>
void main ()
{
clrscr();
printf(“\N Condition : Return Values \n”);
printf(“\n5!=5 :%5d”, 5!=5);
printf(“\n5==5 :%5d”, 5==5);
printf(“\n5>=50 :%5d”, 5>=50);
printf(“\n5<=50 :%5d”, 5<=50);
printf(“\n5!=3 %5d”, 5!=3);
}
LOGICAL OPERATOR: Used to combine the results of two or more conditions.
|| Logical OR (9>2)||(17==7) 1
16
Dr.NNCE I / 02 C / QB
#include<stdio.h>
#include<conio.h>
void main ()
{
int i,j,k;
clrscr();
k=(i=4,j=5);
printf(“k=%d”, k); getch();
}
INCREMENT AND DECREMENT OPERATORS
++ is a increment operator
--is a decrement operator
It is also called as unary operator
Operator Meaning
++x Pre increment
--x Pre decrement
x++ Post increment
x-- Post decrement
if statements
if…..else statements
nested if….else statements
if……else ladder
IF STATEMENTS
The if statement is a decision making statement
Used to control the flow of execution by executing statements when the condition is true (or) false.
Properties of If statement:
If condition is true, it execute true statement
FLOW CHART:
If condition is false it does not do anything.
Condition must be in parenthesis
SYNTAX: EXAMPLE:
if(condition) #include<stdio.h> Condition
{ void main()
True statement; {
} int a;
True statement
printf(“enter the value”);
scanf(“%d”,&a);
True statement
if(a==10)
{
printf(“Both are equal”);
}
}
IF..........................ELSE STATEMENT
It is two way decision making statement
It has two block if & else
18
Dr.NNCE I / 02 C / QB
if block is executed when the condition is true, else block is executed when the condition is false.
SYNTAX: EXAMPLE:
if(condition is true) #include<stdio.h>
{ #include<conio.h>
True statement; void main()
} { condition
else int num,rem;
{ clrscr();
False statement; printf(“Enter ur number”); True statement False statement
} scanf(“%d”,&num);
rem=num%2; if(rem==0)
printf(“the number is even”);
else
printf(“the number is odd”); getch();
}
IF….ELSE LADDER
if(condition 1) #include<stdio.h>
{ main()
Statement 1; {
}
int a,b,c,d;
else if(condition 2)
{ printf(“enter the four numbers”);
Statement 2; scanf(“%d%d%d%d”,&a,&b,&c,&d);
} if((a<b)&&(a<c)&&(a<d))
else if(condition 3) printf(“a is greater”);
{ else
Statement 3; if((b>a)&&(b>c)&&(b>d))
}
printf(“b is greater”);
else
{ else
default-statement; if((c>a)&&(c>b)&&(c>d))
} printf(“c is greater”);
else
printf(“d is greater”);
return(0);
20
Dr.NNCE I / 02 C / QB
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c, option;
printf(“\n 1.Addition”);
printf(“\n 2.subtraction”);
printf(“\n 3.multiplication”);
printf(“\n 4.Division”);
printf(“\n 5.Exit”);
printf(“\n Enter two number”);
scanf(“%d %d”, &a, &b);
printf(“\n Enter your option”)
scanf(“%d”, &option);
switch(option)
{
Case 1:
c=a+b;
printf(“\n Addition=%d” ,c); break;
case 2:
c=a-b;
printf(“\n subtraction=%d”,c);
break;
21
Dr.NNCE I / 02 C / QB
case 3:
c=a*b;
printf(“\n multiplication=%d”,c); break;
case 4:
c=a/b;
printf(“\n division=%d”,c);
break; case 5:
Exit(0); break; default:
printf(“Invalid Choice”);
}
getch();
5. Describe the statements for looping OR Explain the various looping constructs.
Loop:
A loop is defined as a block of statements which are repeatedly executed for certain
number of times. They are three types of loop control statements
for
while
do-while
WHILE LOOP:
It is repetitive control structure used to executed the statements within the body
until the condition becomes false. The while loop is an entry controlled loop
statement
In that condition is evaluated first ,if it is true, then the body of the loop is executed.
while(test condition)
{ #include<stdio.h>
body of the loop; void main()
} {
int number, digit, rev=0; condition
printf(“Enter the number:”);
while(number!=0)
{
digit=number%10;
rev=rev*10+digit;
number=number/10; Body of the loop
}
printf(“%d”,rev);
getch();
}
22
Dr.NNCE I / 02 C / QB
DO….WHILE
Here the condition is checked at the end of the loop.
The do while loop will executed at least one time even if the condition is false initially.
The do-while loop executes until the condition becomes false.
do #include<stdio.h>
{ #include<conio.h>
Statement; void main()
} { Body of the loop
while(condition); int i=2,sum=0;
do
{
sum=sum+i;
i++;
condition
}
while(i<=10)
printf(“sum of the numbers upto 10 is=%d”,sum);
getch();
}
FOR LOOP
It is used to execute set of instructions repeatedly until the condition becomes false.
Initialize counter:
It is used to initialize the counter variable
Test condition:
It is used to test the condition.
Increment / decrement counter:
It is used to Increment / decrement the counter variable
SYNTAX:
23
Dr.NNCE I / 02 C / QB
#include<stdio.h>
#include<conio.h> main()
{
int i, sum=0; for(i=1;i<=10;i++)
{
sum=sum+i;
}
printf(“The addition of numbers upto 10 is %d”,sum); getch();
}
Continue Statement:
The condition statement is used for continuing next iteration of loop statements.
It is useful when we want to continue the program without executing any part of the program.
SYNTAX: EXAMPLE
Continue; #include<stdio.h>
void main()
{
int i,n,sum=0;
for(i=1;i<=5;i++)
{
printf(“Enter any number”);
scanf(“%d”,&n);
if(n<0)
continue;
else
sum=sum+n;
}
printf(“sum is….%d”,sum;);
}
Goto Statement:
This statement does not require any condition.
This statement passes control any where the program
ie., control is transferred to another part of program without testing the application.
Break statement:
24
Dr.NNCE I / 02 C / QB
if (c == 2) {
printf("n is a Prime number");
}
else {
printf("n is not a Prime number");
}
return 0;
25
Dr.NNCE I / 02 C / QB
}
OUTPUT:
Enter any number
n:55 n is not
Prime number
UNIT-II
PART- A
26
Dr.NNCE I / 02 C / QB
&exponent);
while (exponent != 0)
{
result *= base;
--exponent;
}
printf("Answer = %lld", result);
return 0;
}
16. Write a C program to generate Pascal triangle.
#include <stdio.h>
void main()
{
int array[30], temp[30], i, j, k, l, num; //using 2 arrays
printf("Enter the number of lines to be printed: ");
scanf("%d", &num);
temp[0] = 1;
array[0] = 1;
for (j = 0; j < num; j++)
{
printf(" ");
}
printf(" 1\n");
for (i = 1; i < num; i++)
{
for (j = 0; j < i; j++)
printf(" ");
for (k = 1; k < num; k++)
{
array[k] = temp[k - 1] + temp[k];
} Output
array[i] = 1;
for (l = 0; l <= i; l++) Enter the number of lines to be printed: 4
{ 1
printf("%3d", array[l]); 1 1
temp[l] = array[l]; 1 21
} 1331
printf("\n");
}
}
16. Write a C program to find the number of elements in an array.
#include <stdio.h>
29
Dr.NNCE I / 02 C / QB
int main()
{ Output
int array[] = {15, 50, 34, 20, 10, 79, 100};
int n; Size of the given array is 7
n = sizeof(array);
printf("Size of the given array is %d\n",
n/sizeof(int)); return 0;
}
30
Dr.NNCE I / 02 C / QB
String Array
a. String can hold only char data a. Array can hold any data type
b. String size can be changed if it is a b. Array size cannot be changed
character pointer c. The last element of an array is an element
c. The last character of string is a null – of the specific type
„\0‟character d. The length of an array is specified in []
d. The length of the string is the number of at the time of declaration (except char[])
characters + one
PART-B
31
Dr.NNCE I / 02 C / QB
}
printf("\nenter the b
matrix\n"); for(i=0;i<3;i+
+)
{
for(j=0;j<3;j++)
{
printf("b[%d][%d] : ",i+1,j+1);
scanf("%d",&b[i][j]);
}}
printf("\nmultiplication of the 2
matrices :\n\n"); for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3
;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
printf("\t%d",c[i][j]);
}
printf("\n\n");
}
getch();
}
s1.
6 strstr(s1, s2); Returns a pointer to the first occurrence of string s2 in string s1.
7 strlwr() Used to convert string to lower case
8 strupr() Used to convert string to upper case
9 strdup() Used to Used to duplicate a string
duplica
te a
string.
10 strrev() Used to Used to reverse a string
reverse
a string
if(strcmp(d,b)==0)
printf("\nEntered string is a palindrome."); else
printf("\nEntered string is not a palindrome.");
getch();
strupr(d); //String Upper
printf("\n String Upper is %s",d);
strlwr(d); //String Lower
printf("\n String Lower is %s",d);
}
3.Write Program for Numbers in Ascending Order
#include <stdio.h>
void main()
{
int i, j, a, n, number[30]; printf("Enter the value of N \n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] > number[j])
{
a = number[i]; number[i] = number[j];
number[j] = a;
}
}
}
Output:
Enter the value of N 3
Enter the numbers 25
35
45
The numbers arranged in ascending order are given below
25 35 45
#include<stdio.h>
void main()
34
Dr.NNCE I / 02 C / QB
{
int a[10][10],b[10][10],i,j,m,n;
printf("\n enter the row & column of a matrix\n");
scanf("%d %d",&n,&m);
printf("\n enter the elements of a matrix\n"); for(i=0;i<n;
++i)
for(j=0;j<m;++j)
scanf("%d",&a[i][j]);
for(i=0;i<m;++i)
for(j=0;j<n;++j) b[i]
[j]=a[i][j];
printf("Transpose of a matrix”);
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
printf("%d",&b[i][j]);
}
}
Output:
enter the row & column of a
matrix 3 3
enter the elements of a
matrix
213
897
153
Transpose of a matrix-2146584912-2146584908-2146584904-2146584872-2146584868-
2146584864-2146584832-2146584828-2146584824
binary_search();
getch();
}
void binary_search()
{
beg = 0;
end = n-1;
mid = (beg + end) / 2;
while ((beg<=end) && (a[mid]!=item))
{
if (item < a[mid]) end = mid - 1;
else
beg = mid + 1;
mid = (beg + end) / 2;
}
if (a[mid] == item)
printf("\n\nITEM found at location %d", mid+1);
else
printf("\n\nITEM doesn't exist");
}
Output:
Enter size of an array: 3
Enter elements of an array in
sorted form: 25
36
45
Enter ITEM to be
searched: 36 ITEM
found at location 2
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[20];
int i,size,key;
printf("\n\t-- Linear Search --\n\n");
printf("Enter total no. of
elements : ");
scanf("%d",&size);
36
Dr.NNCE I / 02 C / QB
getch();
}
Output:
-- Linear Search --
Enter total no. of elements : 3
Enter 1 element : 25
Enter 2 element : 36
Enter 3 element : 45
Enter the element to be
searched: 36 Element exits
in the list at position : 2
5. Explain in detail about the sorting with an example.
There are many different techniques available for sorting, differentiated by their efficiency and
space requirements. Following are some sorting techniques which we will be covering in next few
tutorials.
1. Bubble Sort
2. Insertion Sort
3. Selection Sort
4. Quick Sort
5. Merge Sort
6. Heap Sort
3.SELECTION SORT
PROGRAM
#include <stdio.h>
int main()
37
Dr.NNCE I / 02 C / QB
{
int arr[10]={6,12,0,18,11,99,55,45,34,2};
int n=10;
int i, j, position, swap;
for (i = 0; i < (n - 1); i++)
{
position = i;
for (j = i + 1; j < n; j++)
{
if (arr[position] > arr[j])
position = j;
}
if (position != i)
{
swap = arr[i];
arr[i] = arr[position];
arr[position] = swap;
}
}
for (i = 0; i < n; i++)
printf("%d\t", arr[i]);
return 0;
}
OUTPUT:0 2 6 11 12 18 34 45 99
C PROGRAM FOR MATRIX SCALING
#include<stdio.h>
int main()
{
int i, j, rows, columns, a[10][10], Number;
printf("\n Enter Number of rows and columns\n");
scanf("%d %d", &i, &j);
printf("\n Please Enter the Matrix Elements \n");
for(rows = 0; rows < i; rows++)
{
for (columns = 0;columns < j;columns++)
{
scanf("%d", &Multiplication[rows][columns]);
} }
printf("\n Enter the Multiplication Value : ");
scanf("%d", &Number);
for(rows = 0; rows < i; rows++)
{
38
Dr.NNCE I / 02 C / QB
39
Dr.NNCE I / 02 C / QB
}
determinant = a[0][0] * ((a[1][1]*a[2][2]) - (a[2][1]*a[1][2])) -a[0][1] * (a[1][0]
return 0;
}
OUTPUT:
Enter the elements of matrix:
123
451
234
The matrix is
1 2 3
4 5 1
2 3 4
#include<stdio.h>
main()
{
int
i,j,a[20]={0},sum=0,n,t,b[20]={0},k=0,c=1,max=0,m
ode; float x=0.0,y=0.0;
printf("\nEnter the limit\n");
scanf("%d",&n);
printf("Enter the set of numbers\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
x=(float)sum/(float)n;
printf("Mean\t= %f",x);
for(i=0;i<n;i++)
{
40
Dr.NNCE I / 02 C / QB
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i]; a[i]=a[j];
a[j]=t;
}
}
} if(n%2==0)
y=(float)(a[n/2]+a[(n-1)/2])/2;
else
y=a[(n-1)/2]; printf("\nMedian\t= %f",y);
for(i=0;i<n-1;i++)
{
mode=0; for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
mode++;
}
}
if((mode>max)&&(mode!=0))
{
k=0;
max=mode;
b[k]=a[i];
k++;
}
else if(mode==max)
{
b[k]=a[i]; k+
+;
}
}
for(i=0;i<n;i++)
{
if(a[i]==b[i])
c++;
}
if(c==n)
printf("\nThere is no mode"); else
{
printf("\nMode\t= ");
for(i=0;i<k;i++)
printf("%d ",b[i]);
41
Dr.NNCE I / 02 C / QB
}
}
OUTPUT:
Median = 3.000000
There is no mode
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &first[i][j]);
printf("Enter the elements of second matrix\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &second[i][j]);
printf("Sum of entered matrices:-\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
sum[i][j] = first[i][j] + second[i][j];
printf("%d\t", sum[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
Enter the number of rows and columns of
matrix 2
2
Enter the elements of first
42
Dr.NNCE I / 02 C / QB
matrix 1 2
34
Enter the elements of second
matrix 5 6
21
Sum of entered
matrices:
6 8
5 5
UNIT-III
PART-A
additionally.
12. Classify the functions based on arguments and return values.
Depending on the arguments and return values, functions are classified into four types.
a. Function without arguments and return values.
b. Function with arguments but without return values.
c. Function without arguments but with return values.
d. Function with arguments and return values.
13. What are address operator and indirection operator? (Nov 2014)
a. The address operator (&) - It represents the address of the variable.
b. Indirection pointer (*) - When a pointer is dereferenced, the value stored at that
address by the pointer is retrieved.
14. What is function prototyping? Why it is necessary? (May 2011)
Many built in functions can be used in C programs. The prototype of these functions is given in
the respective header files. With the help of a function prototype, the compiler can automatically
perform type checking on the definition of the function, which saves the time to delay the program.
15. What are actual parameters and formal parameters? (May 2015)
Actual Parameters: The parameters in the calling program or the parameters in the function call are
known as actual parameters.
Formal Parameters: The parameters in the called program or the parameters in the function header
are known as formal parameters.
16. State the advantage of using functions.
The advantages of using functions are as follows
a. Reduces the size of the program: The length of the source program can be reduced by
using functions at appropriate places.
b. Avoids rewriting the code: As function can be called many times in the program.
Improves re-usability: Same function may be used later by many other programs.
c. Improves re-usability: Same function may be used later by many other programs.
d. Program testing becomes easy: Each function can be tested separately and it is easy to
locate and isolate a faculty function for further investigation.
e. Increases program readability.
17. Differentiate library functions and User-defined functions.
Library Function User- defined functions
a. The library functions are predefined a. The user defined functions are defined by
set of functions. the user.
b. Their task is limited b. Their task is based on user requirement.
c. The user can use the functions but c. The user can modify the function
cannot change or modify them. according to the requirement.
18. What is a recursive function?
If a function calls itself again and again, then that function is called Recursive function. This technique is
known as recursion.
void recursion()
{ recursion(); /* function calls itself */
45
Dr.NNCE I / 02 C / QB
}
int main()
{ recursion(); }
19. What are the applications of recursive function?
The applications of the recursive function are
a. Calculating Fibonacci Series.
b. Calculating Factorial of a program.
20. State the advantage and disadvantage of recursive function.
Advantage of recursive function Disadvantage of recursive function
a. Reduce unnecessary calling of function. a. It is very difficult to trace (debug and
understand)
b. Through recursion one can solve problem
in easy way while its iterative solution is b. Takes a lot of stack space.
very big and complex.
c. Uses more processor time.
21. What is a Pointer? How a variable is declared to the pointer? (Jan 2013, May
2009) Pointer is a variable which holds the address of another variable.
Syntax: data_type *variable_name; Ex.:
int *x, a=5;
x=&a;
46
Dr.NNCE I / 02 C / QB
47
Dr.NNCE I / 02 C / QB
Expression Result
C =*y + 10 Value of c is 30
C = *z + *y Value of c is 50
C = *x + 10 + *y Value of c is 40
C = ++*x Value of c is 11
35. Distinguish between Call by value Call by reference. (May 2014)
Call by Value / Pass by value Call by reference / Pass by reference
a. In Call by value, the value of actual a. In Call by reference, the address of
agreements is passed to the formal actual arguments values is passed to
arguments and the operation is done on formal argument values.
formal arguments. b. Formal arguments values are pointers
b. Formal arguments values are to the actual arguments values.
photocopies of actual arguments values. c. Since address is passed, the changes
c. Changes made in formal arguments valued made in the formal arguments will be
do not affect the actual argument values reflected back to the actual arguments.
PART-B:
1. Explain in detail about the function with an example.
Function:
function is itself a block of code which can solve simple or complex task/calculations.
A function performs calculations on the data provided to it is called "parameter" or "argument". A
function always returns single value result.
Types of function:
1. Built in functions(Library functions)
a.) Inputting Functions.
b.) Outputting functions.
2. User defined functions.
48
Dr.NNCE I / 02 C / QB
a.) fact();
b.) sum();
Parts of a function:
1. Function declaration/Prototype/Syntax.
2. Function Calling.
3. Function Definition.
1.Function Declaration:
Syntax: <return type > <function name>(<type of argument>)
The declaration of function name, its argument and return type is called function declaration.
2.) Function Calling:
The process of calling a function for processing is called function calling.
Syntax: <var_name>=<function_name>(<list of arguments>).
3.) Function definition:
The process of writing a code for performing any specific task is called function definition.
Syntax:
<return type><function name>(<type of arguments>)
{
statement-1> <statement- 2>
return(<vlaue>)
}
Example: program based upon function:
WAP to compute cube of a no. using function. #include<stdio.h>
#include<conio.h> void
main()
{ int c,n; int cube(int); printf("Enter
a no."); scanf("%d",&n); c=cube(n);
printf("cube of a no. is=%d",c);
} int cube(int n)
{
c=n*n*n;
return(c);
}
49
Dr.NNCE I / 02 C / QB
UNIT IV STRUCTURES
PART – A
1. Distinguish between arrays and structures.
Arrays Structures
a. An array is a collection of data items of a. A structure is a collection of data items
same data type. Arrays can only be of different data types. Structures can be
declared. declared and defined.
b. There is no keyword for arrays b. The keyword for structures is struct.
c. An array name represents the address c. A structure name is known as tag. It is
of the starting element a Shorthand notation of the declaration.
d. An array cannot have bit fields d. A structure may contain bit fields
2. Differentiate between Structures and Union.
Structure Union
a. Every member has its own memory. a. All members use the same memory.
b. The keyword used is struct. b. The keyword used is union.
c. All members occupy separate memory c. Different interpretations for the same
location, hence different interpretations of memory location are possible.
50
Dr.NNCE I / 02 C / QB
member definition;
} one or more union variables;
8. How can you access the member of union?
To access any member of a union, use the member access operator (.). The member access
operator is coded as a period between the union variable name and the union member to access. Union keyword is used
to define variables of union type.
9. List the features of structures. (May 2015)
The features of structures are as follows
a. All the elements of a structure are stored at contiguous memory locations
b. A variable of structure type can store multiple data items of different data types under the
one name
10. List the main aspects of working with structure.
a. Defining a structure type (Creating a new type).
b. Declaring variables and constants (objects) of the newly created type.
c. Initializing structure elements
11. What are the two ways of passing a structure to function in C?
a. Passing structure to a function by value
b. Passing structure to a function by address(reference)
12. Write any two advantage of Structure.
a. It is used to store different data types.
b. Each element can be accessed individually.
13.How to initialize a structure variable? Give it’s syntax.
Static storage class is used to initialize structure. It should being and end with curly braces.
Syntax: Static structure tag-field structure variable = {value1, value2,...value 3};
14.Define Anonymous structure.
Unnamed anonymous structure can be defined as the structure definition that does not contain a
structure name. Thus the variables of unnamed structure should be declared only at the time of
structure definition.
15. What are the operations on structures?
The operations on structures are
a. Aggregate operations: An aggregate operation treats an operand as an entity and operates
on the entire operand as whole instead of operating on its constituent members.
b. Segregate operations: A segregate operation operates on the individual members of a
structure object.
16.How to access members of a structure object?
a. Direct member access operator (dot operator)
b. Indirect member access operator(arrow operator)
17.What is the use of ‘period (.)’ in C?
To access any member of a structure, we use the member access operator (.). The member access
operator is coded as a period between the structure variable name and the structure member that we wish to
access.. The members of structure can be accessed individually using period operator. Ex: S1.roll.no;
18. How will you access the structures member through pointers?
52
Dr.NNCE I / 02 C / QB
The structures member can be accessed through pointers by the following ways
a. Referencing pointer to another address to access memory
b. Using dynamic memory allocation
19. Define Nested structure.
Nested structure can be defined as the structure within structure. One structure can be declared inside
other structure as we declare structure members inside a structure. The structure variables can be
a normal structure variable or a pointer variable to access the data.
20.Define array of structures.
Each elements of an array represent a structure variable. If we want to store more array objects in a
structure, then we can declare “array of structure”.
21.Consider the declaration and illustrate the application of size of operator to this structure.
(Nov 2010)
struct student
{ Size of this is 3 bytes:1 byte for name and 2 bytes for integer num.
char name; int
num;
} S;
22. Define Dynamic memory allocation.
The process of changing the array size during program execution is called as dynamic memory
allocation.
23.What are various dynamic memory allocation functions in C?
a. malloc() – Used to allocate blocks of memory I required size of bytes
b. free() – Used to release previously allocated memory space
c. calloc() –Used to allocate memory space for an array of elements
d. realloc()-Used to modify the size of previously allocated memory space
24.What is a volatile and non-volatile memory?
Volatile memory: also known as volatile storage is computer memory that requires power to
maintain the stored information, unlike non-volatile memory which does not require a maintained
power supply. It has been less popularly known as temporary memory. Non-volatile memory:
nonvolatile memory, NVM or non-volatile storage, is computer memory that can retain the stored
information even when not powered.
25.What is Linked List?
A linked list is a set of nodes where each node has two fields „data and „link‟. The data field is
used to store actual piece of information and link field is used to store address of next node.
26. Write down the steps to modify a node in linked
lists. The steps to modify a node in linked lists are
a. Enter the position of the node which is to be modified.
b. Enter the new value for the node to be modified.
c. Replace the original value of that node by a new value
d. Display the message as “The node is modified”.
27. List the operations on singly linked list.
a. Create function
53
Dr.NNCE I / 02 C / QB
b. Display function
c. Search function
d. Insert function
e. Delete function
28. What are the pitfalls encountered in singly linked list?
The singly linked list has only forward pointer and no backward link is provided. Hence the
traversing of the list is possible only in one direction. Backward traversing is not possible.
Insertion and Deletion operations are less efficient because for inserting the element at desired
position the list needs to be traversed. Similarly, traversing of the list is required for locating the
element which needs to be deleted.
29.Define doubly linked list.
Doubly linked list can be defined as a kind of linked list in which each node has two link fields.
One link field stores the address of previous node and the other link field stores the address of next
node.
30.Differentiate between arrays and lists.
In arrays any element can be accessed randomly with the help of index of array, whereas in
lists any element can be accessed by sequential access only. Insertions and deletions of data is
difficult in arrays on the other hand insertion and deletion of data is easy in lists.
31. Write syntax of calloc() and relloc().
Calloc() is for allocating the required amount of memory.
Syntax: Void *calloc(size_t nitems,size_t size)
Relloc() function modifies allocated memory size by malloc() and calloc().
Syntax:Void *relloc(void *ptr,size_t size)
32. What is the use of typedef?
Typedef is used for renaming data types. It redefines the name of existing variable type. It provides
a short and meaningful way to call a data type.
b. File Position
c. File Structure
d. File Access Methods
e. Attributes flag
4. What is the use of functions fseek(), fread(), fwrite() and ftell()?
a. fseek(f,1,i)Move the pointer for file f of a distance 1 byte from location i.
b. fread(s,i1,i2,f)Enter i2 dataitems, each of size i1 bytes from file f to string s.
c. fwrite(s,i1,i2,f)send i2 data items,each of size i1 bytes from string s to file f.
d. ftell(f)Return the current pointer position within file f.
e. The data type returned for functions fread,fseek and fwrite is int and ftell is long int.
5. How is a file opened and file closed?
a. A file is opened using fopen()function. Ex: fp=fopen(filename,mode);
b. A file closed using fclose()function. Ex: fclose(fp);
6. What are the statements used for reading a file? (Nov 2014)
a. FILE*p;Initialize file pointer.
b. fp=fopen(“File_name” ”r”);Open text file for reading.
c. Getc(file_pointer_name);Reads character from file.
d. fgets(str,length,fp); Reads the string from the file.
55
Dr.NNCE I / 02 C / QB
Mode Description
R Opens a text file in reading mode
W Opens or create a text file in writing mode.
A Opens a text file in appended mode.
r+ Opens a text file in both reading and writing mode.
w+ Opens a text file in both reading and writing mode.
56
Dr.NNCE I / 02 C / QB
57
Dr.NNCE I / 02 C / QB
58
Dr.NNCE I / 02 C / QB
59