0% found this document useful (0 votes)
13 views5 pages

Question Bank - CP

Uploaded by

harshvikum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Question Bank - CP

Uploaded by

harshvikum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

University of Mumbai printf(“x”); Option A: 45 What will be the output of the following C code?

Program: _First Year (All Branches) Engineering - SEM-II i--; Option B: 45.0 #include <stdio.h>
Curriculum Scheme: Rev 2019 } Option C: 45.000000 int main()
C-Programming return 0; Option D: 0.000000 {
Question Bank } 18 int a = 1, b = 1, c;
=========================================================== Option A: 2 12 Which among the following is a Conditional Operator in C ? c = a++ + b;
Choose the correct option for following questions. All the Option B: 3 Option A: ?: printf("a=%d, b=%d", a, b);
Q1. Questions are Option C: 4 Option B: :? }
compulsory and carry equal marks Option D: 5 Option C: <=
1. Which storage class is called as default storage class ? Option A: a=1, b=1
Option D: >=
Option A: auto 6. Option B: a=2, b=1
Which among the following is an exit controlled loop ?
Option B: register Option A: Option C: a=2, b=2
for What is the output of the C statement.?
Option C: static Option B: Option D: a=1, b=2
while int main()
Option D: extern Option C: do… while {
Option D: if…else int a=0; What will be the output of the following C code?
What inbuilt function should be used to return a value rounded up to the 13
2. a = 5<2 ? 4 : 3;
next higher integer ? 7 printf("%d",a); #include <stdio.h>
What is another name for 1-D arrays ?
Option A: floor void main()
Option A: Linear arrays return 0;
Option B: malloc } {
Option B: Lists
Option C: puts Option A: 4 int x = 5;
Option C: Horizontal array 19
Option D: ceil Option B: 3 if (x == 5)
Option D: Vertical array
Option C: 5 printf("hi\n");
In the following initialization what is value of A[5] ? int A[10] = {9, 8, 7, 6, 5, 8 Option D: 2 else
3. Which of the following operators takes only integer operands?
4, 3, 2, 1, 0}; Option A: printf("how are u\n");
+
Option A: 5 14 printf("hello\n");
Option B: * Recursion is a process in which a function calls _________.
Option B: 4 Option A: }
Option C: / itself
Option C: 3 Option A: hi
Option D: % Option B: another function
Option D: 2 Option C: hi
main() function Option B:
Option D: hello
What is value of a in following expression? sub program
What is the output for the following code ? 9 how are you
int a = 10 + 4.867; Option C:
int main() 15 hello
Option A: a=10 What is the Format specifier used to print a character in C.?
{ Option D: how are you
Option B: a=14.867 Option A: %s
int a=5,i; Option B:
4. Option C: a=14 %c
i!=a >10; What will be the output of the following C code? (Assuming that we have
Option D: a=4 Option C: %C
printf(“i=%d”,i); entered the value 1 in the standard input).
Option D: %w
return 0; #include <stdio.h>
10 C programs are converted into machine language with the help of ----------.
} void main()
Option A: an editor 16 Which of the following is not a relational operator?
Option A: i=0 {
Option B: an Assembler Option A: >=
Option B: i=10 int ch;
Option C: a compiler Option B: >>
Option C: i=110 printf("enter a value between 1 to 2:");
Option D: an operating system Option C: ==
Option D: i=1 scanf("%d", &ch);
Option D: != 20
switch (ch)
What is the output of the program.? {
How many times will the following while-loop repeat, i.e., how many x are
int main() 17 Which one of the following is a valid C expression? case 1:
printed? int main()
{ Option A: int my_number=1000; printf("1\n");
{
5. 11 float a = 45; Option B: int my-number=1000;
int i = 5; break;
printf("%f", a); Option C: int my@number=1000; printf("hi");
while(i> 0)
return 0; Option D: int @mynumber=1000; default:
{
} printf("2\n");

} Option C: volatile int a = 500, b = 100, c;


} Option D: static What will be the output of the following program? if(!a >= 400)
Option A: 1 int main() b = 300;
1 What will be the output of the following C code? { else
Option B:
hi #include <stdio.h> int i=9; b=b+++b*a/b;
28
Option C: hi void main() while(i++<10) c = 10;
Option D: 2 { printf("%d\n",i); c=b<<1;
m(); return 0; c=c>>b+1;
What will be the output of the following C code? m(); } printf("b = %d c = %d\n", b, c);
24 } Option A: 9 return 0;
#include <stdio.h> void m() Option B: 10 }
int main() { Option C: 1 Option A: B=600, c=3
{ static int x = 5; Option D: 11 Option B: B=600, c=2
21
int i = 0; x++; Option C: B=600, c=1
while (i = 0) printf("%d", x); What will be the output of the following program? Option D: B=600, c=0
printf("True\n"); } int main()
printf("False\n"); Option A: 55 { 33 Which bitwise operator is used for turning off a particular bit in a number?
} Option B: 56 int a,b,c,d,e,f,g,h,k; Option A: |
29
Option A: True Option C: 66 a=8, b=4, c=2, d=1, e=5, f=20; Option B: ^
Option B: False Option D: 67 printf("%d\n",a+b-(c+d)*3%e+f/9); Option C: &
True return 0; Option D: ~
Option C:
False 25 An array Index starts with.? }
Option D: True (Infinite Times) Option A: 0 Option A: 10 What will be the output of the following program?
Option B: 1 Option B: 9 int i;
What will be the output of the following C code? Option C: -1 Option C: 8 int goodday();
Option D: 2 Option D: 20 int main()
#include <stdio.h> {
int main() What will be the output of the following C code? If a is a variable initialized to 1, how many times will the following loop be while(i)
{ #include <stdio.h> executed? {
int x = 0; void main() while((a>0)&&(a<25)) main();
if (x = = 1) 26 { 30 { goodday();
22 34
if (x = = 0) char string[]={'E','X','A','M','\0'}; loopbody i++;
printf("inside if\n"); printf("%s",string); a++; }
else } } printf("Exam\n");
printf("inside else if\n"); Option A: E Option A: 25 return 0;
else Option B: EXAM0 Option B: 24 }
printf("inside else\n"); Option C: EXAM\0 Option C: 20 int goodday()
} Option D: EXAM Option D: 26 {
inside if printf("Goodday");
Option A:
inside else 27 Which one of the following is NOT an identifier? 31 In an array a[2] [2] = {10,20,30,40,50,60}, then a[0] [1] is which element? }
Option B: inside else if _cprogram Option A: 10 Option A: Goodday
Option C: inside if Option A: Option B: Exam Goodday
Option B: 20
Option D: inside else c_program Option C: 30 Option C: Exam
Option B: Option D: Goodday Exam
Option D: 40
The value obtained in the function is given back to the main program by 20cprogram
23 Option C:
using which keyword? What will be the output of the following program?
Option A: new cprogram20 32 int main()
Option B: return Option D:
{
1. Write a program to read Title, Author and Price of 5 books using array of for(j=0;j<n;j++) display(c,m1,n1); expressions or conditions.
structures. Display the records in ascending order of Price. { } Following table shows all the logical operators supported by C language. Assume
#include<stdio.h> printf("\nEnter number="); else
variable A holds 1 and variable B holds 0, then,
struct book scanf("%d",&x[i][j]); {
{ } printf("\nAddition of matrix is not possible..."); Operator Description Example
char title[30],author[30]; } }
float price; } return 0; && Called Logical AND operator. If both the (A && B) is false.
}; } operands are non-zero, then the condition
void display(int x[10][10],int m,int n) becomes true.
int main() { 3. Write a program to check whether a word is palindrome or not.
int i,j; #include<stdio.h> || Called Logical OR Operator. If any of the two (A || B) is true.
{
for(i=0;i<m;i++) #include<string.h> operands is non-zero, then the condition
struct book b[5];
{ void reverse(char s1[100]) becomes true.
int i,j;
for(i=0;i<5;i++) for(j=0;j<n;j++) {
{ ! Called Logical NOT Operator. It is used to !(A && B) is true.
{ int i,j;
printf("%d ",x[i][j]); reverse the logical state of its operand. If a
printf("\nEnter book title, author and price="); char temp;
} condition is true, then Logical NOT operator
scanf("%s%s%f",&b[i].title,&b[i].author,&b[i].price); i=0;
printf("\n"); will make it false.
} j=strlen(s1)-1;
for(i=1;i<5;i++) } while(i<j)
{ } { Bitwise Operators:
for(j=0;j<5-i;j++) temp=s1[i]; The bitwise operators are the operators used to perform the operations on the data at the bit-
{ void add(int a[10][10],int b[10][10],int c[10][10],int m,int n) s1[i]=s1[j]; level.
if(b[j].price>b[j+1].price) { s1[j]=temp; The following table lists the Bitwise operators supported by C. Assume variable 'A' holds 60 and
int i,j;
{ i++; variable 'B' holds 13, then −
temp=b[j]; for(i=0;i<m;i++) j--;
b[j]=b[j+1]; { } Operator Description Example
b[j+1]=temp; for(j=0;j<n;j++) }
} { & Binary AND Operator copies a bit to the result if it (A & B) = 12,
} c[i][j]=a[i][j]+b[i][j]; int main() exists in both operands. i.e., 0000 1100
} } {
} char s1[100],s2[100]; | Binary OR Operator copies a bit if it exists in either (A | B) = 61,
printf("\nSorted List=\n");
} printf("Enter String="); operand. i.e., 0011 1101
for(i=0;i<5;i++)
{ gets(s1);
int main() ^ Binary XOR Operator copies the bit if it is set in one (A ^ B) = 49,
strcpy(s2,s1);
{ operand but not both. i.e., 0011 0001
printf("\n"); reverse(s1);
printf("\nTitle=%s",b[i].title); int a[10][10],b[10][10],c[10][10],m1,n1,m2,n2; if(strcmp(s1,s2)==0) ~ Binary One's Complement Operator is unary and has (~A ) = ~(60),
printf("\nAuthor=%s",b[i].author); printf("Enter number of rows and columns of matrix 1="); { the effect of 'flipping' bits. i.e,. 1100 0011
printf("\nPrice=%f",b[i].price); scanf("%d%d",&m1,&n1); printf("\nPalindrome String");
} printf("Enter number of rows and columns of matrix 2="); } << Binary Left Shift Operator. The left operands value is
scanf("%d%d",&m2,&n2); else A << 2 = 240
return 0; moved left by the number of bits specified by the
if(m1==m2&&n1==n2) { i.e., 1111 0000
} right operand.
{ printf("\nNon palindrome string");
accept(a,m1,n1); } >> Binary Right Shift Operator. The left operands value
2. Implement a program to perform addition of two matrices. A >> 2 = 15
accept(b,m2,n2); return 0; is moved right by the number of bits specified by
#include<stdio.h> i.e., 0000 1111
display(a,m1,n1); } the right operand.
void accept(int x[10][10],int m,int n)
printf("\n");
{
display(b,m2,n2); 4. What are bitwise and logical operators in C ?
int i,j;
add(a,b,c,m1,n1); Logical Operators:
for(i=0;i<m;i++)
printf("\nAddition Matrix=\n"); Logical operators perform logical operations on a given expression by joining two or more
{

5. What are strings and give any four string related functions. for(j=0;j<n;j++) 8. Distinguish between structure and union. The syntax of while loop in c language is given below:
String is defined as character array. {
It is an array of elements with data type as “char” b[j][i]=a[i][j]; Structure Union while(condition)
It can be created using a keyword “struct” It can be created using a keyword “union”
In string, we can store all alphabets, all digits, space and special symbols } {
Example: } Statement
printf("\nTranspose Matrix=\n"); The total memory occupied by structure The total memory occupied by union }
char s1[10];
for(i=0;i<n;i++) variable is summation of memory occupied by variable is same as largest memory
If we want to handle multiple strings or names, we can create 2D string
{ individual structure elements occupied by union elements Flowchart:
Example:
char s1[5][10]; for(j=0;j<m;j++) It takes less memory as compared to
It takes more memory as compared to union
Following are different string handling functions which are present in header file “string.h” { structure
printf("%d ",b[i][j]); We can access individual elements We cannot access individual elements
} simultaneously simultaneously
Function Syntax (or) Example Description It is more efficient as compared to union It is less efficient as compared to structure
printf("\n");
} It takes less execution time as compared to It takes more execution time as compared
return 0; union to structure
strcpy() strcpy(string1, string2) Copies string2 value into string1
}
9. What are the tokens of c language explain with example.
strlen() strlen(string1) returns total number of characters in string1 7. Write a C program to find LCM of two numbers using recursion. Tokens in C is the most important element to be used in creating a program in C. We can
#include<stdio.h> define the token as the smallest individual element in C.
int GCD(int m,int n) There are 4 types of tokens:
strcat() strcat(string1,string2) Appends string2 to string1 1. Keywords:
{
if(n>m) Keywords are those words whose meaning is already known to the compiler
strrev() strrev(string1) It reverses the value of string1 { Example: include, void, main etc.
return GCD(n,m); 2. Variables and Constants:
} Variables are those entities whose value can change throughout the program
strcmp() strcmp(string1, string2) Returns 0 if string1 and string2 are the same; else if(n==0) Constants are those entities whose value remain same throughout the program
less than 0 if string1<string2; greater than 0 if { 3. Identifiers:
string1>string2 return m; Identifiers are names given to variables or functions 11. Write a program to print Fibonacci series.
} Rules of Identifiers: #include<stdio.h>
else 1. It should not be keyword int main()
{ 2. It can contain all alphabets (A-Z and a-z), all digits (0-9) and special character underscore {
6. Implement a program to find transpose of a matrix. (_)
return GCD(n,m%n); int n,a,b,i,c;
#include<stdio.h>
} 3. It should either start with alphabet or underscore printf("Enter number of terms=");
int main()
} 4. Its maximum allowed length is 31 scanf("%d",&n);
{
int main() 5. It is case sensitive a=1;
int a[10][10],b[10][10];
{ 4. Operators: b=1;
int m,n,i,j;
int a,b,g,l; Operators are used to perform different operations printf("%d\t%d",a,b);
printf("Enter number of rows and columns of matrix=");
printf("Enter 2 numbers="); C contains following 3 categories of operators: for(i=1;i<=n-2;i++)
scanf("%d%d",&m,&n);
scanf("%d%d",&a,&b); 1. Unary Operators: {
for(i=0;i<m;i++)
g=GCD(a,b); It contains those operators which are used with single operand c=a+b;
{
l=(a*b)/g; 2. Binary Operators: printf("\t%d",c);
for(j=0;j<n;j++)
printf(“\nLCM=%d”,l); It contains those operators which are used with two operands a=b;
{
return 0; 3. Ternary Operators: b=c;
printf("\nEnter Number=");
} It contains those operators which are used with three operands }
scanf("%d",&a[i][j]);
return 0;
}
10. Explain while loop with example. }
}
while Statement:
for(i=0;i<m;i++)
It is entry-controlled statement. It first checks the condition and then execute the statement; the
{
process gets continued until the condition becomes false.
12. Write a program using recursion to find factorial of a number. scanf("%d%s%d%d%d",&s2.roll,&s2.name,&s2.mk.p,&s2.mk.c,&s2.mk.m); for(k=0;k<n1;k++) d=(a>b)?((a>c)?a:c):((b>c)?b:c);
#include<stdio.h> t1=s1.mk.p+s1.mk.c+s1.mk.m; { printf("Largest number=%d",d);
int fact(int n) t2=s2.mk.p+s2.mk.c+s2.mk.m; c[i][j]=c[i][j]+a[i][k]*b[k][j]; return 0;
{ if(t1>t2) } }
if(n==0) { }
{ printf("\nName of student having highest total=%s",s1.name); } 16. Explain the term recursion. Write a program to find the power of x raised
return 1; } } to n that is: xn, using recursive function.
} else int main() Recursion is the process of repeating items in a self-similar way. In programming languages, if a
else { { program allows you to call a function inside the same function, then it is called a recursive call
{ printf("\nName of student having highest total=%s",s2.name); int a[10][10],b[10][10],c[10][10],m1,n1,m2,n2; of the function.
return n*fact(n-1); } printf("Enter number of rows and columns of matrix 1=");
} return 0; scanf("%d%d",&m1,&n1); Program:
} } printf("Enter number of rows and columns of matrix 2="); #include<stdio.h>
int main() scanf("%d%d",&m2,&n2); int power(int x,int n)
{ 14. Write a C program to perform multiplication of two matrices. if(n1==m2) {
int n,f; #include<stdio.h> { if(n==0)
printf("Enter number="); void accept(int x[10][10],int m,int n) accept(a,m1,n1); {
scanf("%d",&n); { display(a,m1,n1); return 1;
f=fact(n); int i,j; accept(b,m2,n2); }
printf("\nFactorial=%d",f); for(i=0;i<m;i++) display(b,m2,n2); else
return 0; { multiply(a,b,c,m1,n1,m2,n2); {
} for(j=0;j<n;j++) printf("\nMultiplication Matrix=\n"); return x*power(x,n-1);
{ display(c,m1,n2); }
13. Explain nested structures with examples. printf("\nEnter Number="); } }
A nested structure in C is a structure within structure. One structure can be declared inside scanf("%d",&x[i][j]); else int main()
another structure in the same way structure members are declared inside a structure. We can } { {
access the member of the nested structure by } printf("\nMultiplication of matrix is not possible"); int x,n,p;
Outer_Structure_Variable.Inner_Structure_Variable.Inner_Structure_Element } } printf("Enter x and n=");
Example: return 0; scanf("%d%d",&x,&n);
#include<stdio.h> void display(int x[10][10],int m,int n) } p=power(x,n);
struct student { printf("\nResult=%d",p);
{ int i,j; 15. Explain conditional operator used in C language with proper example. return 0;
int roll; for(i=0;i<m;i++) }
char name[30]; { The conditional operator is also known as a ternary operator. The conditional statements are
struct marks for(j=0;j<n;j++) the decision-making statements which depends upon the output of the expression. It is
{ represented by two symbols, i.e., '?' and ':'. 17. Explain following functions with example
{
int p,c,m; printf("%d ",x[i][j]); sqrt(), fabs(), pow(), ceil(), floor()
} As conditional operator works on three operands, so it is also known as the ternary operator.
}mk;//Inner structure ka variable outer structure ka element maana jaata hai
}; printf("\n"); Function Description
} Syntax of a conditional operator
Expression1? expression2: expression3; returns the square root of given number.
int main() }
{ void multiply(int a[10][10],int b[10][10],int c[10][10],int m1,int n1,int m2,int n2)
Example: #include <stdio.h>
struct student s1,s2; {
#include<stdio.h> #include <math.h>
int t1,t2; int i,j,k;
int main() sqrt(n) int main ()
printf("Enter roll number,name and marks of physics,chemistry and maths of student for(i=0;i<m1;i++)
{ {
1="); {
int a,b,c,d; printf("Square root of %d is %f\n", 4, sqrt(4) );
scanf("%d%s%d%d%d",&s1.roll,&s1.name,&s1.mk.p,&s1.mk.c,&s1.mk.m); for(j=0;j<n2;j++)
printf("Enter 3 numbers="); printf("Square root of %d is %f\n", 5, sqrt(5) );
printf("\nEnter roll number,name and marks of physics,chemistry and maths of student {
scanf("%d%d%d",&a,&b,&c); return 0;
2="); c[i][j]=0;

} 18. Write a program to print the following pattern. } int main()


A {
returns the power of given number. struct player p[10];
BB 20. Write a Program to calculate and display sum of all the elements of the
matrix. int i,j;
#include <stdio.h> CCC for(i=0;i<10;i++)
#include<stdio.h>
#include <math.h> DDDD {
int main()
pow(a,b) int main () printf("\nEnter player name, team name and batting average=");
{
{ #include<stdio.h> scanf("%s%s%f",&p[i].pname,&p[i].tname,&p[i].average);
int a[10][10];
printf("Power of %d and %d is %f", 2, 3, pow(2,3) ); int main() }
int m,n,i,j,s1=0;
return 0; {
printf("Enter rows and columns of matrix=");
} int n,i,j; printf("\nPlayer List=\n");
scanf("%d%d",&m,&n);
char ch; for(i=0;i<10;i++)
rounds up the given number. It returns the integer value which is for(i=0;i<m;i++)//Row
printf("Enter number of lines="); {
greater than or equal to given number. {
scanf("%d",&n); printf("\n");
for(j=0;j<n;j++)//Column
ch='A'; printf("\nPlayer Name=%s",p[i].pname);
#include <stdio.h> {
for(i=1;i<=n;i++) printf("\nTeam Name=%s",p[i].tname);
#include <math.h> printf("\nEnter Number=");
ceil(n) { printf("\nBatting Average=%f",p[i].average);
int main () scanf("%d",&a[i][j]);
for(j=1;j<=i;j++) }
{ }
{ return 0;
printf("Ceil of %f is %d", 2.5, ceil(2.5) ); }
printf("%c",ch); }
return 0; for(i=0;i<m;i++)
}
} {
printf("\n"); 22. Write a program to accept three numbers from the user and display the
rounds down the given number. It returns the integer value which is for(j=0;j<n;j++)
ch++;
less than or equal to given number. { greatest of three using the conditional operator.
}
printf("%d ",a[i][j]); #include<stdio.h>
return 0;
#include <stdio.h> } int main()
}
#include <math.h> printf("\n"); {
floor(n) } int a,b,c,d;
int main () 19. Write a program to find largest element of an 1D array.
{ for(i=0;i<m;i++) printf("Enter 3 numbers=");
#include<stdio.h> { scanf("%d%d%d",&a,&b,&c);
printf("Floor of %f is %d", 2.5, floor(2.5) ); int main()
return 0; for(j=0;j<n;j++) d=(a>b)?((a>c)?a:c):((b>c)?b:c);
{ { printf("Largest number=%d",d);
} int a[100],n,i,max; s1=s1+a[i][j]; return 0;
returns the absolute value of given number. printf("Enter number of elements="); } }
scanf("%d",&n); }
#include <stdio.h> for(i=0;i<n;i++) printf("\nSum of all array elements=%d",s1);
#include <math.h> { 23. Write a program to display the following for the user specified number
return 0;
printf("\nEnter element="); }
of lines.
int main () scanf("%d",&a[i]); *
{ } **
21. Define a structure called player with data members as player name, team
fabs(n) int a, b; max=a[0];
name, batting average. Store and display the information of at least 10 ***
a = 1234; for(i=1;i<n;i++)
{ players. ****
b = -344;
printf("The absolute value of %d is %f\n", a, fabs(a)); if(max<a[i]) #include<stdio.h> *****
printf("The absolute value of %d is %f\n", b, fabs(b)); { struct player ******
max=a[i]; {
return(0); } char pname[30],tname[30]; #include<stdio.h>
} } float average; int main()
printf("\nLargest Number=%d",max); }; {
return 0; int n,i,j,s;
printf("Enter number of lines="); 25. Write a program in C to find out the power of x raised to n (x^n), using 27. Write a program in C to find the reverse of a given string without using {
scanf("%d",&n); non-recursive function. inbuilt string function. s=s+a[i];
s=n-1; #include <stdio.h> #include<stdio.h> }
for(i=1;i<=n;i++) int main() #include<string.h> average=s/10.0;
{ { void reverse(char s1[100]) printf("\nAVERAGE=%f",average);
for(j=1;j<=s;j++) int base, exponent; { return 0;
{ int power = 1; int i,j; }
printf(" "); int i; char temp;
} printf("Enter base: "); i=0; 29. Write a program to store and display at least 10 records of the name, roll
for(j=1;j<=i;j++) scanf("%d", &base); j=strlen(s1)-1; number and fees of a student using structure.
{ printf("Enter exponent: "); while(i<j)
printf("*"); scanf("%d", &exponent); { #include<stdio.h>
} for(i=1; i<=exponent; i++) temp=s1[i]; struct student
printf("\n"); { s1[i]=s1[j]; {
s--; power = power * base; s1[j]=temp; int rno;
} } i++; char name[30];
return 0; printf("%d ^ %d = %d", base, exponent, power); j--; float fees;
} return 0; } };
} }
24. Write a program to check if the entered number is prime number or not. int main()
#include<stdio.h> 26. Write a program in C to find the smallest of N elements using an array. int main() {
int main() #include<stdio.h> { struct student s[10];
{ int main() char s1[100],s2[100]; int i,j;
int n,i,count=0; { printf("Enter String="); for(i=0;i<=9;i++)
printf("Enter number="); int a[100],n,i,min; gets(s1); {
scanf("%d",&n); printf("Enter number of elements="); reverse(s1); printf("\nEnter Rol no Name and Fees=");
scanf("%d",&n); printf("\nReverse String=%s",s1); scanf("%d%s%f",&s[i].rno,&s[i].name,&s[i].fees);
for(i=1;i<=n;i++) for(i=0;i<n;i++) return 0; }
{ { } for(i=0;i<=9;i++)
if(n%i==0) printf("\nEnter element="); {
{ scanf("%d",&a[i]); 28. Write a program to accept a set of 10 numbers and print the numbers printf("Rollno=%d Name=%sfees=%f\n",s[i].rno,s[i].name,s[i].fees);
count++; } using arrays. Find the average of these integers.
} min=a[0]; }
#include<stdio.h>
} for(i=1;i<n;i++) return 0;
int main()
if(count==2) { }
{
{ if(min>a[i]) int a[10],n,i,s=0;
printf(“\nIt is a prime number”); {
}
float average; 30. Explain five arithmetic operators used in C language with proper
min=a[i]; for(i=0;i<10;i++)
else examples.
} {
{ } printf("\nEnter element="); Arithmetic Operators:
printf(“\nIt is not a prime number”); } printf("\nSmallest Number=%d",min); scanf("%d",&a[i]);
} return 0; } An arithmetic operator performs mathematical operations such as addition, subtraction,
return 0; } printf("\nArray Elements are\n");
}
for(i=0;i<10;i++) multiplication, division etc on numerical values (constants and variables).
{
printf("\t%d",a[i]);
}
for(i=0;i<10;i++)

#include <stdio.h> ch++;


Operator Meaning of Operator
// copying str1 to str2 sp--;
strcpy(str2, str1); int addNumbers(int n); }
+ addition or unary plus return 0;
puts(str2); // C programming int main() { }
- subtraction or unary minus
return 0; int num; 34. Write a C-program to create array of structures in order to store details
* multiplication } printf("Enter a positive integer: "); of almost 100 books. The book details are book name, book price, book
scanf("%d", &num);
page number and book author name.
/ division Output printf("Sum = %d", addNumbers(num));
return 0;
C programming #include<stdio.h>
}
% remainder after division (modulo division) struct books
{
#include <stdio.h> int addNumbers(int n) {
int pno;
int main() if (n != 0)
Merging of two strings. char bn[30],an[30];
{ return n + addNumbers(n - 1);
The strcat() function concatenates the destination string and the source string, and the result is float p;
int a = 9,b = 4, c; else
stored in the destination string. };
return n;
c = a+b; }
int main()
printf("a+b = %d \n",c);
#include <stdio.h> {
c = a-b;
#include <string.h> 33. Write a program to print the following pattern. (Note- Not only 4 lines, it struct book b[100];
printf("a-b = %d \n",c);
c = a*b;
int main() { should print N lines taken from the user.) int i,j;
char str1[100] = "This is ", str2[] = "programiz.com"; for(i=0;i<=99;i++)
printf("a*b = %d \n",c); A
{
c = a/b;
// concatenates str1 and str2 B B printf("\nEnter Book record=");
printf("a/b = %d \n",c);
// the resultant string is stored in str1. scanf("%s%f%d%s",&b[i].bn, &b[i].p, &b[i].pno, &b[i].an);
c = a%b; C C C
strcat(str1, str2); }
printf("Remainder when a divided by b = %d \n",c); D D D D for(i=0;i<=99;i++)
puts(str1); {
return 0; #include<stdio.h>
puts(str2); printf("Book Name %s Price %f Page no %d Author %s",b[i].bn, b[i].p, b[i].pno, b[i].an);
} int main() }
{ return 0;
return 0;
int n,i,j,sp,k; }
}
31. Explain String function for the following operations with example. char ch;
printf("Enter number of lines=");
Copy string from source to destination Output 35. Write a program that will accept two-dimensional square matrix and find
scanf("%d",&n);
Merging of two strings ch='A'; the sum of diagonal elements.
This is programiz.com
The strcpy() function copies the string pointed by source (including the null character) to the sp=n-1
for(i=1;i<=n;i++) #include<stdio.h>
destination. programiz.com
{ int main()
The strcpy() function also returns the copied string. for(k=1;k<=sp;k++) {
{ int i, j, m, n, a[10][10], Sum = 0;
The strcpy() function is defined in the string.h header file.
32. Explain the term recursion. Write a program to find summation of n printf(“ “); printf("\n Please Enter Number of rows and columns : ");
} scanf("%d %d", &m, &n);
#include <stdio.h> numbers using recursion. printf("\n Please Enter the Matrix Elements \n");
#include <string.h> for(j=1;j<=i;j++)
Recursion is the process of repeating items in a self-similar way. In programming languages, for(i = 0; i < m; i++)
{
if a program allows you to call a function inside the same function, then it is called a recursive {
int main() { printf("%c ",ch);
call of the function. for(j = 0;j < n;j++)
char str1[20] = "C programming"; }
printf("\n"); {
char str2[20];
scanf("%d", &a[i][j]); double ceil(double x) return 0; temp = *x; /* save the value of x */
} • This function returns the smallest integral value not less than x. } *x = *y; /* put y into x */
} iv) puts() void swap(int x, int y) { *y = temp; /* put temp into y */
Syntax:
for(i = 0; i < m; i++) int puts(const char* str) int temp; return;
{ • To use the puts function, you need to include the <stdio.h> library in the }
for(j = 0;j < n;j++) program. temp = x; /* save the value of x */
{ • The puts function writes the provided argument to the output stream and x = y; /* put y into x */ Output:
if(i==j) appends a newline character at the end. y = temp; /* put temp into y */ Before swap, value of a : 100
{ v) getchar() Before swap, value of b : 200
Sum = Sum + a[i][j]; Syntax: return; After swap, value of a : 200
} int getchar(void) } After swap, value of b : 100
} • The getchar function is part of the <stdio.h> header file in C.
} • The function reads the input as an unsigned char; then it casts and returns as an Output: 38. Write a C program to find GCD of two numbers using recursion.
printf("\n The Sum of Diagonal Elements of a Matrix = %d", Sum ); int or an EOF. Before swap, value of a : 100 Program:
Before swap, value of b : 200 #include<stdio.h>
return 0; After swap, value of a : 100 int gcd(int m,int n)
} 37. What are the different ways of parameter passing to a function? Explain After swap, value of b : 200 {
with examples. if(m==0)
Call by Reference: return n;
36. Explain the use of following in-built functions of C-language by giving Parameters can be passed in two ways:
1. Call by Value. The call by reference method of passing arguments to a function copies the address of an else if(n>m)
suitable programming examples and also mention their respective header argument into the formal parameter. Inside the function, the address is used to access the
2. Call by Reference. gcd(n%m,m);
files in which they are defined. actual argument used in the call. It means the changes made to the parameter affect the passed else
i) getch() Call by Value: argument.To pass a value by reference, argument pointers are passed to the functions just like gcd(n,m);
ii) pow() The call by value method of passing arguments to a function copies the actual value of an any other value. }
argument into the formal parameter of the function. In this case, changes made to the Example: int main()
iii) ceil()
parameter inside the function have no effect on the argument. By default, C programming uses #include <stdio.h> {
iv) puts()
call by value to pass arguments. int m,n,g;
v) getchar() Example: int main () { printf("enter the value of m and n\n");
#include <stdio.h> scanf("%d %d",&m,&n);
i) getch(): /* local variable definition */ g=gcd(m,n);
Syntax: /* function declaration */ int a = 100;
void swap(int x, int y); int b = 200; printf("gcd of %d and %d is %d\n",m,n,gcd(m,n));
int getch(void);
return 0;
• The getch() is a predefined non-standard function that is defined in conio.h printf("Before swap, value of a : %d\n", a );
int main () { }
header file. printf("Before swap, value of b : %d\n", b );
• We use a getch() function in a Cprogram to hold the output screen for some
/* local variable definition */ 39. Write a C program to implement month name by accepting month
time until the user passes a key from the keyboard to exit the console screen. /* calling a function to swap the values */
int a = 100; number from user. ( Use switch case)
• Parameters: The getch() function does not accept any parameter from the user. swap(&a, &b);
int b = 200; Program:
Return value: It returns the ASCII value of the key pressed by the user as an
input. #include<stdio.h>
printf("Before swap, value of a : %d\n", a ); printf("After swap, value of a : %d\n", a );
ii) pow() int main()
printf("Before swap, value of b : %d\n", b ); printf("After swap, value of b : %d\n", b );
Syntax: {
double pow(double x, double y) int n;
/* calling a function to swap the values */ return 0;
• The first argument is a base value and second argument is a power raised to the printf("enter month no\n");
swap(a, b); }
base value. scanf("%d",&n);
void swap(int *x, int *y) {
• The pow() function is defined in math.h header file. printf(" Name of the month number %d is:\n",n);
printf("After swap, value of a : %d\n", a ); switch(n)
iii) ceil() int temp;
printf("After swap, value of b : %d\n", b ); {
Syntax:
case 1:printf("January\n");break;

case 2:printf("February\n");break; for(i=0;i<10;i++)


No. Key Structure Array
case 3:printf("March\n");break; {
case 4:printf("April\n");break; printf("%d ",a[i]); as input as it only supports
case 5:printf("May\n");break; } same type data variables.
case 6:printf("June\n");break; return 0;
case 7:printf("July\n");break; } Performance Structure due to use defined On other hand in case of
case 8:printf("August\n");break; data type become slow in Array access and searching
case 9:printf("September\n");break; 41. Give the difference between entry and exit controlled loop with an performance as access and of element is faster and
7
case 10:printf("October\n");break; example. searching of element is hence better in
case 11:printf("Novembern");break; slower in Structure as performance.
case 12:printf("December\n");break; compare to Array.
default:printf("invalid month number\n");break; Entry Control Loop Exit Control Loop
} Entry control loop checks condition The exit control loop first executes the body of the
return 0; first and then loop and
} body of the loop will be executed. checks condition at last.
The body of the loop may or may The body of the loop will be executed at least once
40. Write a C program to accept 10 integers from the user and arrange them not be executed at all. because the condition is checked at last
in ascending order and display them. for, while are an example of an
Do…while is an example of an exit control loop.
Program: entry control loop

#include<stdio.h> 42. Differentiate between arrays and structures.


int main()
{
No. Key Structure Array
int a[10],i,j,temp;
Definition Structure can be defined as Array is a type of data
printf("Enter the 10 elements in an Array\n"); a data structure which can structure used as container
for(i=0;i<10;i++) hold variables of different which can hold variables of
{ 1
types. same type and do not
scanf("%d",&a[i]); support multiple data type
} variables.
printf("Before sorting\n");
for(i=0;i<10;i++) Memory Memory allocation for input While in case of array the
{ Allocation data in structure does not input data stored in
printf("%d ",a[i]); 2 necessary to be in contiguous memory .
} consecutive memory
location.
for(i=0;i<9;i++)
{ Accessibility In order to access the On other hand in case of
for(j=0;j<9;j++) element in Structure we Array we can access the
3
{ require to have the name of element by index.
if(a[j]>a[j+1]) that element.
{
Instantiation Structure object can be On other hand in case of
temp=a[j];
a[j]=a[j+1]; 5 created after declaration Array we can't create its
later in the program. object after declaration.
a[j+1]=temp;
}
DataType Structure supports multiple On other hand in case of
}
6 data-type variables as input. Array we can't have
}
different data-type variable
printf("\nAfter sorting\n");

You might also like