You are on page 1of 95

# qwertyuiopasdfghjklzxcvbnmqwertyui opasdfghjklzxcvbnmqwertyuiopasdfgh jklzxcvbnmqwertyuiopasdfghjklzxcvb nmqwertyuiopasdfghjklzxcvbnmqwer tyuiopasdfghjklzxcvbnmqwertyuiopas NAGPUR INSTITUTE OF TECHNOLOGY, NAGPUR dfghjklzxcvbnmqwertyuiopasdfghjklzx DEPARTMENT OF ELECTRONICS & TELECOMMUNICATION cvbnmqwertyuiopasdfghjklzxcvbnmq

Question Bank with Solutions C & DATA STRUCTURE (III ETC) wertyuiopasdfghjklzxcvbnmqwertyuio Compiled By: pasdfghjklzxcvbnmqwertyuiopasdfghj klzxcvbnmqwertyuiopasdfghjklzxcvbn mqwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasdf ghjklzxcvbnmqwertyuiopasdfghjklzxc vbnmqwertyuiopasdfghjklzxcvbnmrty uiopasdfghjklzxcvbnmqwertyuiopasdf ghjklzxcvbnmqwertyuiopasdfghjklzxc

## Ms. Apurva Fulzele

Email: apuravafulzele@nitengg.edu.in

IIISemETC(C&DatastructureQuestionBank) Q. 1) Write a program to find roots of a quadratic Ax2+Bx+C=0, A, B, C are entered through the keyboard and are non zero integers. # include <stdio.h> # include <conio.h> # include <math.h> void main() { float a,b ,c,x1,x2,term; clrscr(); printf(enter the value of a,b,c); scanf(%f %f %f,&a,&b,&c); term=(b*b)-(4*a*c); if(term<0) { printf(roots are imaginary); } if(term==0) { printf(roots are real & equal); x1=-b/(2*a); x2=x1; printf(%f ,%f,x1,x2); } if(term>0) { printf(roots are real & different); x1=(-b+sqrt(term))/(2*a); x2=(-b-sqrt(term))/(2*a); printf(%f ,%f,x1,x2); } getch(); } ******************************************************************

## 1|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) Q. 2) Write a program to prime numbers between 1 to 100. # include <stdio.h> # include <conio.h> void main() { int num ,i,flag=0; clrscr(); for(num=1;num<=100;num+1) { flag=0; for(i=1;i<=num-1;i++) { If(num%i==0) { flag=1; break; } } if(flag==0) { printf(\n %d,num); } getch(); } ******************************************************************

## 2|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) Q. 3) Write a program to calculate. F(x)=x-x3/3!+x5/5!-x7/7!---------------n. # include <stdio.h> # include <conio.h> void main() { float x,nr,term,sum=0; int dr=1,I,n,p=1; clrscr(); printf(enter the value of x & n); scanf(%f ,%d,&x ,&n); nr-x; while(i<=n) { term=nr/dr; sum=sum+term; nr=nr*(-x*x); p=p+2; dr=dr*(p-1)*p; i=i+1; } printf(f(x)=%f, sum); getch(); } ******************************************************************

## 3|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) Q. 4) Write a program to print the Fibonacci series. 0 1 1 2 3 5 8 ______n. # include <stdio.h> # include <conio.h> void main() { int f1=0,f2=1,f3,i=3,n; clrscr(); printf(enter the value of n); scanf(%d,&n); if(n==1) { printf(%d,f1 ); } if(n==2) { printf(%d,f2 ); } If(n>2) { printf(\n%d %d ,f1,f2); while(i<=n) { f3=f1+f2; f2=f3; i=i+1; } } getch(); } ******************************************************************

## 4|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) Q. 5) Write a program to accept an integer and to find the sum of the digits Of the integer reducing it to a single digit.(assume 3 digit). # include <stdio.h> # include <conio.h> void main() { int num,d1,d2,d3,sum; clrscr(); printf(enter the number); scanf(%d,&num); d1=num%10; num=num/10; d2=num%10; num=num/10; d3=num%10; num=num/10; sum=d1+d2+d3; printf(%d,sum); getch(); } ****************************************************************** Q. 6) Write a program to test test whether accepted year is leap year or not. # include <stdio.h> # include <conio.h> void main() { int year; clrscr(); printf(enter the year); scanf(%d,&year); if(year%100==0)

## 5|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) { If(year%400==0) { printf(\n leap year); } else { printf(\n not a leap year); } } else { If(year%4==0) { printf(\n leap year); } else { printf(\n not a leap year); } } getch(); } ****************************************************************** Q.7). Write a program to find the factorial of a given number using the concept of recursiveness. # include <stdio.h> # include <conio.h> void main() { int num,f; clrscr();

## 6|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) printf(enter the number); scanf(%d,&num); f=fact(num); printf(\n factorial=%d,f); getch(); } Int fact(int n) { int f1; if(n==0) { return(0); } else { f1=n*fact(n-1); return(f1); } } ****************************************************************** Q.8). Write a program to add first n terms of the following series. 1/1!+2/2!+3/3!+4/4!+----------------Here ! stands for factorial of a number.

# include <stdio.h> # include <conio.h> void main() { float term,sum=0; int dr=1,i=1,n,nr=1; clrscr(); printf(enter the value of n); scanf(%d,&n);

## 7|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) while(i<=n) { term=nr/(dr*1.0); sum=sum+term; i=i+1; dr=dr*i; } printf(%f,sum); getch(); } ****************************************************************** Q. 9) Write a program to check whether the given character is uppercase, lowercase or non alphabetic character. # include <stdio.h> # include <conio.h> void main() { int ch; clrscr(); printf(enter character); scanf(%d,&ch); if(ch>=97&&ch<=122) { printf(lowercase character); } else { if(ch>=65&&ch<=90) { printf(uppercase character); }

## 8|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) else { printf( non- alphabetic character ); } getch(); } ****************************************************************** Q.10). Write a program to find the number of series. X+2x+3x+4x+5x+-----------+nx. # include <stdio.h> # include <conio.h> void main() { float term,sum=0,x; clrscr(); printf(enter the value of x & n); scanf(%f ,%d,&x ,&n); term=x; while(i<=n) { sum=sum+x; term=term+x; i=i+1; } printf( %f,sum ); getch(); } ******************************************************************

## 9|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) Q. 11) Write a program to check whether an integer n is prime or not. # include <stdio.h> # include <conio.h> void main() { int num,count=0,i; clrscr(); printf(enter the number); scanf(%d,&num); for(i=1;i<=num;i++) { if(num%i==0) { count++; } } If(count==1||count==2) { printf(\n prime number); } else { printf( \n not prime number); } getch(); } ******************************************************************

## 10|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) vowel or consonant. # include <stdio.h> # include <conio.h> void main() { char ch; clrscr(); printf(enter any character); scanf(%c,&ch); if(ch=a|| ch=e|| ch=i|| ch=o|| ch=u|| ch=A|| ch=E|| ch=I|| ch=O|| ch=O) { printf(character is vowel); } else { printf(character is consonant); } getch(); } ****************************************************************** Q. 13) Write a program to determine whether given character is vowel or consonant using switch case. # include <stdio.h> # include <conio.h> void main() { char ch; clrscr(); printf(enter any character); scanf(%c,&ch); switch(ch) { case a:

## 11|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) case e: case i: case o: case u: case A: case E: case I: case O: case U: printf(character is vowel); break; default: printf(character is consonant); } getch(); } ****************************************************************** 14) Write a program to determine whether entered number is even or odd. # include <stdio.h> # include <conio.h> void main( ) { int num; clrscr(); printf(enter the number); scanf(%d,&num); if(num%2==0) { printf(number is even number); } else {

## 12|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) printf(number is odd number); } getch(); ****************************************************************** Q.15. Write a function swap() to swap the contents of two variables. # include <stdio.h> # include <conio.h> void swap(int p, int q) { int temp; temp=p; p=q; q=temp; } void main() { Int a, b; a=10; b=20; clrscr(); printf(\nA=%d,B=%d,a,b); swap(a,b); printf(\nA=%d,B=%d,a,b); getch(); }

## 13|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) using switch case. i) whether roots are imaginary. ii) roots are real & equal. iii) roots are real & unequal. # include <stdio.h> # include <conio.h> # include <math.h> void main() { float a,b ,c,x1,x2,term,num; clrscr(); printf(enter the value of a,b,c); scanf(%f %f %f,&a,&b,&c); printf(enter the choice); scanf(%d,&num); term=(b*b)-(4*a*c); switch(num) case 1: if(term<0) { printf(roots are imaginary); } case 2: if(term==0) { printf(roots are real & equal); x1=-b/(2*a); x2=x1; printf(%f ,%f,x1,x2); } case 3:

## 14|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) if(term>0) { printf(roots are real & different); x1=(-b+sqrt(term))/(2*a); x2=(-b-sqrt(term))/(2*a); printf(%f ,%f,x1,x2); } case 4: { exit(0); } getch(); } ****************************************************************** Q.17. Write a program to print ASCII table. # include <stdio.h> # include <conio.h> void main() { int i; clrscr(); for(i=0;i<=255;i++) { printf(\n %c,i); } getch(); } ******************************************************************

## 15|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) Q.18. Write a program to concatenate two strings entered by the user. # include <stdio.h> # include <conio.h> # include <string.h> void main() { char s1[20],s2[10]; clrscr(); printf(\n enter first string); scanf(%s,s1); printf(\n enter second string); scanf(%s,s2); strcat(s1,s2); printf(\n concatenated string=%s,s1); getch(); } ****************************************************************** Q.19. Write a program to test whether the number is prime number function should return 1 if the number is prime number else 0, if not a prime number. # include <stdio.h> # include <conio.h> void main() { int num,r; clrscr(); printf(enter any number); r=isprime(num); if(r==1) {

## 16|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) printf(number is prime number); } else { printf(number is not prime number); } getch(); } isprime(int i) {int i,count=0; for(i=1;i<=n;i++) { if(n%i==0) { count++; } } if(count==1||count==2) { return(1); } else { return(0); } getch(); } ******************************************************************

## 17|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank)

Q.20. Write a program which has following options. i)to find given number is prime or not. ii) to find given character is lowercase, uppercase ,or any other special character. iii)to find factorial of given number using recursive function . iv)exit from the program. # include <stdio.h> # include <conio.h> int fact(int); void main() { int ch,num,i,f,count; clrscr(); printf(enter any number); scanf(%d,&num); printf(prime number); printf(lowercase, uppercase); printf(factorial); printf(exit); printf(enter choice); scanf(%d,&ch); switch(ch) { case 1: count=0; for(i=1;i<=num;i++) { if(num%i==0) { count++; }

## 18|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) } If(count==1||count==2) { printf(\n prime number); } else { printf( \n not prime number); } break; case 2: if(ch>=97&&ch<=122) { printf(lowercase character); } else { if(ch>=65&&ch<=90) { printf(uppercase character); } break; case 3: f=fact(num); printf(\n factorial=%d,f); break; case 4: exit(0); } getch(); } int fact(int n) { int f1; if(n==0)
19|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) { return(0); } else { f1=n*fact(n-1); return(f1); } } ****************************************************************** Q.21. Write a switch statement that will examine value of 10 integers variable called flag And print one of the following msg depending upon the value assign on it (flag). i) HOT, if flag has value of 1 ii) COLD, if flag has value of 1 iii) OUT of range, if flag has other value # include <stdio.h> # include <conio.h> void main() { int flag; clrscr(); printf(enter the value of flag); scanf(%d,& flag); switch(flag) case 1: printf(HOT); break; case 2: printf(COLD); break; default:

## 20|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) printf(OUT of range); break; } getch(); } ****************************************************************** Q.22. Write a program to count the chars, spaces,tabs and newlines in a file. # include <stdio.h> # include <conio.h> void main() { FILE *fp; char ch; int nol=0,not=0,nob=0,noc=0; fp=fopen(PR1.C,r); while(1) { ch=fgetc(fp) if(ch==EOF) break; noc++; if(ch== ) nob++; if(ch==\n) nol++; if(ch==\t) not++; } fclose(fp); printf(\n Number of characters=%d,noc); printf(\n Number of blanks=%d,nob); printf(\n Number of tabs=%d,not);

## 21|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) printf(\n Number of lines=%d,nol); getch(); } ****************************************************************** Q.23. Write a program to the contents of one file into another file. # include <stdio.h> # include <conio.h> # include <stdlib.h> void main() { FILE *fp; char ch; fp=fopen(PR1.C,r); if(fs==NULL) { puts(Cannot open source file); exit(1); } ft=fopen(PR2.C,w); if(ft==NULL) { puts(Cannot open target file); fclose(fs); exit(2); } while(1) { ch=fgetc(fs); if(ch==EOF) break; else

## 22|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) fputc(ch,ft); } fclose(fs); fclose(ft); getch(); } ****************************************************************** Q.24.Define two structures Date and Student. The structure date consists of three members i.e day, month & year and define a second structure student name, student D.O.B and student age as structure member. Write a program to show the use of structure within structure. # include <stdio.h> # include <conio.h> # include <stdlib.h> void main() { struct Date { int dd,mm,yy; }; struct student { char name[10]; struct Date D.O.B; int age; }s1; clrscr(); printf(enter name); scanf(%s,s1.name); printf(enter date in dd/mm/yy format); scanf(%d %d %d, &s1.DOB.dd, &s1.DOB.mm, &s1.DOB.yy);

## 23|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) printf(enter age); scanf(%d,&s1.age); printf(\n enter name=%s); printf(\n enter age=%d); printf(\n date of birth=%d%d%d); getch(); } ****************************************************************** Q.25. Write a program to reverse a string entered by the user without using standard library function. # include <stdio.h> # include <conio.h> # include <stdlib.h> # include <string.h> void main() { char S1[20],S2[10]; int i,j; clrscr(); printf(\n enter string); scanf(%s,S1); i=0; j=0; while(S1[i]!=\0) { i++; } S2[i]=\0; i--; while(i>=0) { S2[j]=S1[i]; j++;

## 24|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) i--; } printf(\n reverse=%s,s2); getch(); } ****************************************************************** Q.26. Write a program to check whether entred string is a palindrome string or not. # include <stdio.h> # include <conio.h> # include <stdlib.h> # include <string.h> void main() { char S1[10],S2[10]; int flag; clrscr(); printf(\n enter string); scanf(%s,S1); strcpy(S2,S1); strrev(S2); flag=strcmp(S1,S2); if(flag==0) printf(\n palindrome string); else printf(\n not palindrome string); getch(); } ******************************************************************

## 25|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) Q.27. Write a program to reverse a string entered by the user using standard library function. # include <stdio.h> # include <conio.h> # include <stdlib.h> # include <string.h> void main() { char S1[10]; int i,j; clrscr(); printf(\n enter 1st string); scanf(%s,S1); strrev(S1); printf(\n reverse string=%s,S1); getch(); } ****************************************************************** Q.28. Write a program to sort 15 integers numbers in an array using bubble sort method.(ascending order) # include <stdio.h> # include <conio.h> # include <stdlib.h> # define MAX 15 void main() { int a[MAX]; int i, j,temp; clrscr(); printf(\n enter %d elements, MAX); for(i=0;i<MAX;i++)

## 26|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) { scanf(%d,&a[i]); } printf(\n entered elements are); for(i=0;i<MAX;i++) { printf(%d,a[i]); } for(i=0;i<=MAX-2;i++) { for(j=0;j<=MAX-2-i; j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1] =temp; } } } printf(\n sorted array is); for(i=0;i<MAX;i++) { printf(\n%d,a[i]); } getch(); } ******************************************************************

## 27|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) Q.29. Write a program to sort 15 integers numbers in an array using selection sort method.(ascending order) # include <stdio.h> # include <conio.h> # include <stdlib.h> # define MAX 15 void main() { int a[MAX]; int i, j,temp; clrscr(); printf(\n enter %d elements, MAX); for(i=0;i<MAX;i++) { scanf(%d,&a[i]); } printf(\n entered elements are); for(i=0;i<MAX;i++) { printf(%d,a[i]); } for(i=0;i<=MAX-1;i++) { for(j=i+1;j<=MAX; j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j] =temp; } }

## IIISemETC(C&DatastructureQuestionBank) } printf(\n sorted array is); for(i=0;i<MAX;i++) { printf(\n%d,a[i]); } getch(); } ******************************************************************

Q.30. Write a program to sort 15 integers numbers in an array using bubble sort method.(descending order) # include <stdio.h> # include <conio.h> # include <stdlib.h> # define MAX 15 void main() { int a[MAX]; int i, j,temp; clrscr(); printf(\n enter %d elements, MAX); for(i=0;i<MAX;i++) { scanf(%d,&a[i]); } printf(\n entered elements are); for(i=0;i<MAX;i++) { printf(%d,a[i]); }

## 29|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) for(i=0;i<=MAX-2;i++) { for(j=0;j<=MAX-2-i; j++) { if(a[j]<a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1] =temp; } } } printf(\n sorted array is); for(i=0;i<MAX;i++) { printf(\n%d,a[i]); } getch(); } ****************************************************************** Q.31. Write a program to sort 15 integers numbers in an array using selection sort method.(ascending order). # include <stdio.h> # include <conio.h> # include <stdlib.h> # define MAX 15 void main() { int a[MAX]; int i, j,temp; clrscr(); printf(\n enter %d elements, MAX); for(i=0;i<MAX;i++)

## 30|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) { scanf(%d,&a[i]); } printf(\n entered elements are); for(i=0;i<MAX;i++) { printf(%d,a[i]); } for(i=0;i<=MAX-1;i++) { for(j=i+1;j<=MAX; j++) { if(a[i]<a[j]) { temp=a[i]; a[i]=a[j]; a[j] =temp; } } } printf(\n sorted array is); for(i=0;i<MAX;i++) { printf(\n%d,a[i]); } getch(); } ******************************************************************

## 31|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) Q.32. Write a program to check whether the entered matrix is symmetry of a matrix or not .(if matrix and its transpose is same). # include <stdio.h> # include <conio.h> # include <math.h> void main() { int a[3][3],b[3][3]; int i,j,sum; clrscr(); printf(\n enter matrix elements); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf(%d,&a[i][j]); } } sum=0; for(i=0;i<3;i++) { for(j=0;j<3;j++) { b[j][i]=a[i][j]; } } printf(\original matrix ); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf(%d,a[i][j]);

## 32|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) } printf(\n); } printf(\transpose matrix ); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf(%d,b[i][j]); } printf(\n); } for(i=0;i<3;i++) { for(j=0;j<3;j++) { if(a[i][j]==b[i][j]) { printf(symmetric matrix); } else { printf(symmetric matrix); } } } getch(); } ******************************************************************

## 33|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) Q.33. Write a program to calculate the area of a circle. # include <stdio.h> # include <conio.h> # include <math.h> void main() { float pi=3.1415,area,radius; clrscr(); printf(enter the radius); scanf(%f,& radius); area=pi*radius*radius; printf(area of a circle=%f,area); getch(); } ****************************************************************** Q.34. Write a program to calculate the area of a triangle. # include <stdio.h> # include <conio.h> # include <math.h> void main() { float a,b,c,area,s; clrscr(); printf(enter 3 sides of a triangle); scanf(%f %f %f,& a,& b,& c); s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)); printf(area of a triangle =%f,area); getch(); } ******************************************************************

## 34|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank)

Q.35. Write a program to convert temperature from 0C to 0F using formula:0F=9/5*C+32. # include <stdio.h> # include <conio.h> # include <math.h> void main() { float C,F; printf(enter the temperature in degree centigrate); scanf(%f,&C); F=(C*9/5)+32; printf(%f is temperature in degree farenheits,F); getch(); } ****************************************************************** Q.36. Write a program to store the following information about 50 students. Roll no., name , marks of 5 subjects & average of marks, using structure.print this information in the form of table after evaluating of marks. # include <stdio.h> # include <conio.h> # include <math.h> void main() type def struct student { char name[10]; int roll_ no; int m1, m2, m3,m4,m5; float avg; }; struct student S[50]; int i;

## 35|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) clrscr(); for(i=0;i<50;i++) { printf(\n enter the name & roll _no. of the student); scanf(\n %s ,%d,S[i].name,&S[i].roll _no.); printf(\n enter the marks of five subjects); scanf(%d,%d,%d,%d,%d& S[i].m1,& S[i].m2,& S[i].m3,& S[i].m4,& S[i].m5); } for(i=0;i<50;i++) { S[i].avg=((S[i].m1+ S[i].m2+ S[i].m3+ S[i].m4+ S[i].m5)/5.0); } for(i=0;i<50;i++) { printf(\nName=%s\troll_no.=%d\tm1=%d\tm2=%d\tm3=%d\tm4=%d\tm5=%d\ta verage=%f,S[i].name,S[i].roll_no.,S[i].m1, S[i].m2, S[i].m3, S[i].m4 ,S[i].m5,S[i].avg); } getch(); } ****************************************************************** Q.37 Solve the following equation by using gauss siedal method. 15x+4y+z=13 2x+8y+2z=12 4x+y+10z=13 Solution: Given equation are 15x+4y+z=13 2x+8y+2z=12 4x+y+10z=13 x=13-4y-z/15 (1)

## IIISemETC(C&DatastructureQuestionBank) y=12-2z-2x/8 z=13-4x-y/10 (2) (3)

Putting y=0 & z=0 in equation (1) x=13/15=0.866 x=0 .866 Putting x=0 .866& z=0 in equation (2) y=12-(2*0)-(2*0.866)/8; y=1.2835 Putting y=1.2835& x=0.866 in equation (3) z=13-4(0.866)-(1.2835)/10 z =0.8250 Now 2nd iteration Putting y=1.2835& z =0.8250 in equation (1) x=13-4(1.283)-(0.8250)/10; x=0.7043 Putting x=0.7043& z =0.8250in equation (2) y=12-(2*0.8250)-2(0.7048)/8 y=1.1127 Putting y=1.1127& x=0.7043 in equation (3) z=13-4(0.7048)-1.1127/10 z=0.9068 3rd iteration Putting y=1.1127& z=0.9068 in equation (1) x=13-4(1.1127)-0.9068/15 x=0.5094 Putting x=0.5094& z=0.9068 in equation (2) y=12-2(0.968)-2(0.5094)/8 y=1.145 Putting y=1.145& x=0.5094 in equation (3) z=13-(4*0.5094)-1.145)/10 z=0.98174 4th iteration Putting y=1.145& z=0.98174 in equation (1)
37|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) x=13-4(1.145)- 0.98174/15 x=0.495 Putting x=0.495& z=0.98174in equation (2) y=12-2(0.98174)-2(0.495)/8 y=1.1320 Putting y=1.1320& x=0.495in equation (3) z=13-(4*0.495)- 1.1320)/10 z=0.988 5th iteration Putting y=1.1320 & z=0.988in equation (1) x=13-4(1.1320)- 0.988/15 x=0.498 Putting x=0.498& z=0.988 in equation (2) y=12-2(0.988)-2(0.498)/8 y=1.190 Putting y=1.190 & x=0.498 in equation (3) z=13-(4*0.498 )- 1.190)/10 z=0.988 iteration 1 2 3 4 5 x 0.866 0.7043 0.50 0.49 0.498 y 1.283 1.112 1.14 1.13 1.19 z 0.8250 0.906 0.98 0.988 0.988

****************************************************************** Q.38.Write a program for gauss siedel method using following equations 15x+4y+z=13 2x+8y+2z=12 4x+y+10z=13 # include <stdio.h> # include <conio.h>

## 38|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) # include <math.h> float fx(float ,float); float fy(float ,float); float fz(float ,float); void main() { float x=0,y=0,z=0,xnew,ynew,znew; int i, ic=0; printf(iteration x y z \n); for(i=0;i<=5;i++) { xnew=fx(y,z); x=xnew; ynew=fy(x,z); y=ynew; znew=fz(y,z); z=znew; } Printf(%d %f %f %f\n,ic,xnew,ynew,znew); ic++; } getch(); } ****************************************************************** Q.39 Solve the following equation by newton raphson method. x3-2x-5=0 Solution: Let us consider f(x)= x3-2x-5=0 By N-R method Xn+1=Xn-f(x)/f(x) (1) 2 f(x)=3x -2 f(0)=-5

## 39|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) f(1)=-6 f(2)=-1 f(3)=16 now x0=2 sustitute n=0 in equation (1) x1=2-f(x0)/f(x0) =2-(x03-2x0-5)/(3x02-2) x1=1.2 sustitute n=1 in equation (1) x2=x1-f(x1)/f(x1) =2.1-(x13-2x1-5)/(3x12-2) x2=2.0945 sustitute n=2 in equation (1) x3=x2-f(x2)/f(x2) =2.0945-(x23-2x2-5)/(3x22-2) x3=2.0945 ****************************************************************** Q.40. Write a program for newton raphson method for equation x^5+5x+1. # include <stdio.h> # include <conio.h> # include <math.h> float fna(int); float fnd(int); void main() { int x0,x,xa,xb; float ad,f1,f2,d,x1,x2; printf(enter value of xa); scanf(%d,&xa); if(xa>0) xb=xa-1; else xb=xa+1;

## 40|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) f1=fna(xa); f2=fna(xb); d=f1*f2; if(f1==0) { printf(value of xa=%d,xa); getch(); } if(f2==0) { printf(value of xb=%d,xb); getch(); } x0=xa; x1=xb; again:x2=x1-(fna(x1))/(fnd(x1); ad=(float)(abs(int)(x2)-(int)(x1)); if(ad<=0.0009) { printf(value of x=%f,x2); getch(); } else { x1=x2; goto again; } float fna(int x) { float f; f=pow(x,5)+5*x+1; return(f); } float fnd(int xx) {
41|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) float fd; fd=5*pow(x,4)+5; return(fd); } ****************************************************************** Q.41.Given an equation dy/dx=y-x & y(0)=2 calculate y(0.1)& y(0.2).Using Runga Kutta method(2nd order) solution: Given: dy/dx=y-x y(0)=2 let f(x)=y-x (1) y0=2 & x0=0 we know that , x0=x0+h K1=hf(x0,y0) K2=hf(x0+h,y0+k1) Yn+1=yn+(K1+K2)/2 We have to find y(0.1) & y(0.2) i.e y1=? at x1=0.1 y2=? at x2=0.2 h=x1-x0 h=0.1-0=0.1 h=0.1 First iteration K1=hf(x0,y0) K1=0.1(2-0) K1=0.2 K2=hf(x0+h,y0+k1) =0.1((2+0.2)-(0+0.1))

## 42|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) K2=0.21 For n=0 in equation (1) y1=y0+(K1+K2)/2 =2+(0.2+0.21) =1.205 y1=1.205 y(0.1)= 1.205 second iteration K1=hf(x1,y1) K1=0.1(1.205-0.1) K1=0.1105 K2=hf(x1+h,y1+k1) =0.1((1.205+0.1)-(1.1+0.11)) K2=0.0105 For n=1 in equation (1) y2=y1+(K1+K2)/2 =1.205+(0.115+0.0105) =0.665 y2=0.665 y(0.2)= 0.665 ****************************************************************** Q.42. Write a program for Runga Kutta method(2nd order) for equation dy/dx=y-x & y(0)=2 calculate y(0.1)& y(0.2). # include <stdio.h> # include <conio.h> # include <math.h> float fy(float x,float y); void main() { float K1,K2,x0,y0,x1,y1;

## 43|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) float h=0.1; float y; clrscr(); printf(enter the value at which y is required); scanf(%f,&y); for(x0=0;y0=2;x0<=y;x0=x0+h) { K1=h*fy(x0,y0); K2=h*fy(x0+h,y0+k); y1=y0+(K1+K2)/2; y0=Y1; } printf(the value of y(%f) is %f,y,y1); } float fy(float x,float y) { return(y-x); } ****************************************************************** Q.43. Write a program to search an element present in an array of size 10.using linear search. # include <stdio.h> # include <conio.h> void main() { int a[10]; int i,num,flag; clrscr(); printf(\n enter elements); for(i=0;i<10;1++) { scanf(%d,&a[i]); }

## 44|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) printf(\n enter element to be search); scanf(%d,&num); flag=0; for(i=0;i<10;i++) { if(a[i]==num) { flag=1; printf(\n elements found at position %d,i); break; } } if(flag==0) { printf(elements not found); } getch(); } ****************************************************************** Q.44. Write a program to search an element present in an array of size 10.using binary search. # include <stdio.h> # include <conio.h> # define MAX 10 void main() { int a[MAX]; int i, flag=0,low,high,mid,num; clrscr(); printf(\n enter elements); for(i=0;i< MAX;1++)

## 45|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) { scanf(%d,&a[i]); } printf(\n enter element to be search); scanf(%d,&num); low=0; high=MAX-1; while(low<=high) { mid=(low+high)/2; If(a[mid]==num) { flag=1; printf(\n elements found at position %d,mid); break; } if(a[mid]>num) { high=mid-1; } else { low=mid+1; } } if(flag==0) { printf(elements not found); } getch(); } ****************************************************************** Q.45.Explain algorithm of runga kutta method (2nd order).

## 46|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) Ans: Step 1: y=f(x,y) y(x0)=y0 is given find y(x)=? Step 2: To find the y at given x use the formulae x1=x0+h y1=y0+(K1+K2)/2 Where K1=hf(x0,y0) K2=hf(x0+h,y0+k1) ****************************************************************** Q.46 Describe the history of C Language. Ans: Dennis Ritchie designed and implemented the first C compiler on a PDP-11 (a prehistoric machine by today's standards, yet one which had enormous influence on modern scientific computation). The C language was based on two languages i.e BCPL, written by Martin Richards, and B, written by Ken Thompson in 1970 for the first UNIX system on a PDP-7. The original ``official'' C language was the ``K & R'' C, the nickname coming from the names of the two authors of the original ``The C Programming Language''. In 1988, the American National Standards Institute (ANSI) adopted a ``new and improved'' version of C, known today as ``ANSI C''. This is the version described in the current edition of ``The C Programming Language ANSI C''. The ANSI version contains many revisions to the syntax and the internal workings of the language, the major ones being improved calling syntax for procedures and standardization of most (but, unfortunately, not quite all!) system libraries. ****************************************************************** Q.47 List the Keywords of C language? Ans: if else for while do switch case break continue default goto static extern auto void return long char float double struct union typedef const near far enum signed unsigned register int volatile

## 47|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) ****************************************************************** Q.48 Explain scanf() function? OR Q.49. Describe the type of characters that would occur in a format string of scanf()? Ans: Scanf() is a input function present in the library of C language. With the help of this function user can enter any combinations of numerical values, single character and strings. Syntax: scanf (<control string >,arg1,arg2,arg3argn); where, control string represent format specifiers. arg1, arg2, ag3 argn represent address of individual variable. Format specifier used to scan integer, character, float and string should be different. It is always prefix with a symbol % and a character. Following are the format specifiers used to scan corresponding data. %d===> integer %f===> float %c===> character %s===> string Example: void main() { int a,b,c; clrscr(); printf (Enter any 3 numbers); scanf(%d %d %d, &a,&b,&c); printf(Answer=%d,a+b+c); getch(); } In the above example control string of scanf function consists of 3 format specifier to scan 3 integer values from the user. Scanf function will store input value in given variables of which

## 48|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) address is specified i.e. first number will store in variable a, second number will store in variable b and so on. ****************************************************************** Q.50. Explain printf () function? OR Q.50. What are the components of format specifier of printf function? Ans: Printf () is a output function present in the library of C language. With the help of this function user can display any combinations of numerical values, single character and strings on the output screen. syntax : printf(<control string>,arg1,arg2,arg3.argn); where, control string represent format specifiers. Arg1,arg2,arg3argn represent individual variable. Format specifier used to print integer, character, float and string should be different. It is always prefix with a symbol % and a character. Following are the format specifiers used to scan corresponding data. %d===> integer %f ===> float %c ===> character %s ===> string Example: void main() { int=10; float b=12.25; char c =A; clrscr (); printf(%d%f%c,a,b,c); getch(); } In the above example control string of printf function comnsists of 3 format specifier %d, %f and %c to print integer, float and character values on the output

## 49|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) screen. First access specifier retrieve integer number from first argument i.e. a, second access specifier retrieve float value from second argument i.e. b and third access specifier retrieve character value from the third argument i.e. ****************************************************************** Q.51. Write short note on operators in C? OR Q.51. What are the different types of operators available in C? Ans: Operator is a predefined sign used to perform specific operator + is used to perform addition of two values. Different operators of C language are Arithmetic Operators + for addition operation . - for substraction operation . * for multiplication operation. / for division operation. % for modulus operation. = for assignment operation. Logical operators && AND operator || OR operator ! NOT operator Relational operators > greater than operator < less than operator >= greater than equal to operator <= less than equal to operator == equal to operator != not equal to operator Conditional or Ternary operator syntax: (Condition ? Expression1: expression2); Symbol of conditional or ternary operator is? If the condition given in the conditional operator is true than expression1 will execute otherwise expression2 will execute, user can use ternary operator in place of expression1 and expression2 i.e. nesting of ternary operator is allowed. Pointer operator (*)

## 50|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) & is known as pointer operator. Symbolic name of is ampercend which is used to find address of specified variable. Structure operator .(dot) operator is known as structure operator. We can access member variable of structure by using dot (.) operator. -> is also known as structure operator. If the structure variable is pointer then we can access member variable of structure by using -> operator. ****************************************************************** Q.52. Distinguish between global and local variables? Ans. Local variable global variable 1.Scope of local variable is local to the 1. Scope of global variable is global i.e. within block in which it is defined. the complete program. 2. Default inital value of local variable 2. Default initial value of global variable is is garbage value. zero. ****************************************************************** Q.53. Distinguish between compiler and interpreter? Ans: Compiler Interpreter 1. Compiler convert complete source 1. Interpreter convert complete source code Code into machine language in in to machine language line by line. 2. Compiler takes less time to compile. 2. Interpreter takes more time to interpret. 3. More disk space is required during 3. Less disk space is required during Compilation. Interpretation. 4. Example: C,C++,VC++ 4. Example: Visual Basic , Java. ****************************************************************** Q.54. Explain Identifiers, Constants and Keywords? Ans: Identifiers: Identifiers are the variable name given to location in the memory of computer. Where different constants are stored. Constant: Fixed and known value is called as constant. There are 4 types of constants 1. Integer Constant: Fixed and known number written without any decimal point is known as Integer constant. Example: 10,23,45,890,0,1000 2. Floating constant: Fixed and known number wrtten with any decimal point is known as Integer
51|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) constant. Example: 1,2,3,5,0,002,5.67,1.2e-7 3. Character constant: Single character written within single quotes() is known as character constant. Example: A,5,\$,0,P 4. String constant: one or more than one character written within double quotes() is known as string constant. Example: abc,hello,hi,234,shri420,Praveen. Keywords: Keywords are the words whose meaning is already defined in the C compiler. These words cannot be used as variable name. ****************************************************************** Q.55. What is the necessity of loops in C? Explain for loop with syntax. Ans. If you want to repeat the same blocks of code over and over you have two choices. Copy and paste each block or you can use a loop. In C there are three different types of loops: for, while, and dowhile.. for( variable declaration/initialization; condition; variable update) { //Code to be repeated } So as you can see there are three parts needed to use a for loop. The first part is the variable declaration/initialization. Here you can either declare a new variable and assign it a value or use an existing variable. In C you can use any integer based data type (int, char, etc), in other languages there are other types of for loops (for each) where you can use other data types. Second, the condition tells the program to exit the loop when the conditional expression evaluates to false. Finally the variable update does just that, updates the variables. Generally you would put something like x++, x = x + 10 but if you really wanted to you could call another function to update the variable. ******************************************************************

## 52|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) Q.56. Write down the difference between while and do-while loop Ans: While Do-while 1. General syntax: 1. General syntax: Initialization; Initialization; While(condition) do {{ ---------------- ------------------------------- ------------------------------- ---------------updation; updation; } }while (condition); 2. Condition checking operation 2. Condition checking operation is is performed before the execution performed after the execution of Of C statements. C statements. 3. If initial condition is false then 3. If initial condition is false then loop loop will not execute. will execute minimum one time. 4. Example: 4. Example: void main() void main() {{ int i; int i; i=5; i=5; while(i<=3) do {{ Printf (\n%d,i); print (\n%d,i); i ++; i ++; } }while (i<=3); getch (); getch (); }} Output: Output: Not Output 5 ****************************************************************** Q.57. Difference between break and continue? Ans: break continue 1. syntax: 1. syntax break continue

## 53|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) 2.break keyword is allowed to 2.continue keyword is allowed to use in Use in for loop, while loop, for loop, while loop and do-while loop. do- while loop and switch case statement. 3. break statement terminates 3. continue statement ignores current execution The execution of loop. Statements of loop and perform execution for Next step of loop. 4. Example: 4. Example: main() main() {{ int i; int i; clrscr(); clrscr(); for(i=1;i<=5;i++) for(i=1;i<=5;i++) {{ if(i==3) if(i==3) break; continue; prinf (\n%d,i); printf(\n%d,i); }} getch(); getch(); }} output: output: 11 22 4 5 ****************************************************************** Q.58. Difference between call by value and call by reference? Ans: Call by value Call by reference 1. In call by value method value is 1. In call by reference method address of passed from calling function to variable is passed from calling function called function. to called function. 2. Simple variable is used to access 2. Pointer variable is used to access value Value of variable. Of variable. 3. Function cannot change the value 3. Function can change the value of Of argument variable. Argument by using pointer variable. 4. Return statement is used to return 4. Pointer variable is used to return value Value from called function to calling from called function to calling function. Function. Also return statement is used to return Value.

## 54|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) ****************************************************************** Q.59. What is Function? What are the types of function? Ans: Function: Function is the independent block of program which is written to Perform (accompanied) the given task. There are two types of functions. 1. Standard library function. These are those functions which are already present in the header file of C Compiler. We can readily use standard functions by including their corresponding . Header files: conio.h,stdio.h,stdlib.h,math.h,string.h,dir.h,process.h,dos.h. Functions : printf(); , scanf(); , clrscr(); , gotoxy() ; , getch(); , strcpy(); 2. User defined function: These are those functions which are defined by user to perform his own task. These functions are not present in the header file of C compiler. ****************************************************************** Q.60. Define the following terms. 1. Function 2. Recursion 3. Pointer 4.Array 5.String 6. Structure Ans: 1. Function: Function is the independent block of program which to perform (accompanied) the given task. 2. Recursion: Recursion is the calling of function again and again from its own called function. 3.Pointer: Pointer is a variable containing address as well as value at address. 4. Array: Array is a collection of elements of similar data type stored in contiguous memory locations. 5. String: Array is a collection of elements of similar data type stored in contiguous memory locations. String is an array of characters. 6. Structure: Structure is a collection of elements of dissimilar data type stored in contiguous memory locations. ******************************************************************

## 55|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) Q.61. What are the data types available in C Language? Ans. Data type Format Specifier Bytes Range 1. signed int or %d or %i 2 -32768 to 32767 int 2. unsigned int %u 2 0 to 65535 3. long int %ld 4 -2147483648 to 2147483647 4. long unsigned %lu 4 0 to 4294967295 int 5. signed char %c 1 -128 to 127 Or char 6. unsigned char %c 1 0 to 255 7. float %f 4 3.4e-38 to 3.4e38 8. double %lf 8 1.7e-308 to 1.7e308 9. long double %Lf 10 1.7e-4932 to 1.7e4932 ****************************************************************** Q.62. Explain storage classes in C language? Ans. Variables storage class gives following information. 1. where the variable would be stored. 2. what will be the initial value of the variable. 3. what is the scope of variable. 4. what is the life of variable. There are four storage classes in C: 1. Automatic storage class. 2. Register storage class. 3. Static storage class. 4. External storage class. Automatic storage class Storage - memory Default initial value - garbage value Scope - local to the block in which variable is defined Life - Till the control remains within the block in the variable is defined. Example1: main() { auto int i; printf(\n%d,i);

## 56|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) } Above program will print garbage value on the output screen. Example2: This example shows the importance of scope of auto variable. main() { auto int i =1; { auto int i =2; { auto int i =3; printf(\n%d,i); } printf(\n%d,i): } printf(\n%d,i): } output 3 2 1 Register storage class storage -CPU registers. default initial value - garbage value scope - local to the block in which the variable is defined life - Till the control remains within the block in which the variable is defined. CPU registers are usually 16 bits registers and therefore can hold only value which require Minimum 1 byte and maximum 2 bytes, cannot store float, long or double value, which require 4 and 8 bytes. A value stored in a CPU register can always be accessed faster than the one which is stored in memory. Total CPU registers are 14 therefore it is not necessary that every variable will get memory. From the CPU registers. Example1: main() { Register int i;
57|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) for (i=1; i<=10; i++) printf (%d\t,i); } output:1 2 3 4 5 6 7 8 9 10 Static storage class storage - memory. default initial value - zero scope - local of the block in which variable is defined life - value of the variable persists between different function calls. Example1: main() { clrscr(); Add(); Add(); Add(); Getch(); } Add() { static int i; printf(%d\t,i); } output: 0 1 2 External storage class storage - memory default initial value - zero scope - global life - Till the end of program Example1: int x=21; main() { extern int x; printf (\n%d,x); output: 21 Example2: int x;
58|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) main() { x++; printf(%d\t,x); callme(); getch(); } Callme() { x++; printf(%d\t,x); } output: 1 2 ****************************************************************** Q. 63 Explain the following two statements? # include filename # include <filename> Ans: #include filename This command would look for the given file in the current directory as well as the specified list of directories as mentioned in the include search path that have been set up. # include<filename> This command would look for the given file in the specified list of directories as mentioned in the include search path that have been set up. ****************************************************************** Q. 64. What is functions in C? Ans: A function in C language is a block of code that performs a specific task. It has a name and it is reusable i.e. it can be executed from as many different parts in a C Program as required. It also optionally returns a value to the calling program So function in a C program has some properties discussed below. Every function has a unique name. This name is used to call function from main() function. A function can be called from within another function.

## 59|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) A function is independent and it can perform its task without intervention from or interfering with other parts of the program. A function performs a specific task. A task is a distinct job that your program must perform as a part of its overall operation, such as adding two or more integer, sorting an array into numerical order, or calculating a cube root etc. A function returns a value to the calling program. This is optional and depends upon the task your function is going to accomplish. Suppose you want to just show few lines through function then it is not necessary to return a value. But if you are calculating area of rectangle and wanted to use result somewhere in program then you have to send back (return) value to the calling function. C language is collection of various inbuilt functions. If you have written a program in C then it is evident that you have used Cs inbuilt functions. printf, scanf, clrscr etc. all are Cs inbuilt functions. You cannot imagine a C program without function. Structure of a Function A general form of a C function looks like this: <return type> FunctionName (Argument1, Argument2, Argument3) { Statement1; Statement2; Statement3; } An example of function. int sum (int x, int y) { int result; result = x + y; return (result); } ****************************************************************** Q. 65 : What are advantages of using Functions? Ans: There are many advantages in using functions in a program they are: 1. It makes possible top down modular programming. In this style of programming, the high level logic of the overall problem is solved first while the details of each lower level functions is addressed later.

## 60|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) 2. The length of the source program can be reduced by using functions at appropriate places. 3. It becomes uncomplicated to locate and separate a faulty function for further study. 4. A function may be used later by many other programs this means that a c programmer can use function written by others, instead of starting over from scratch. 5. A function can be used to keep away from rewriting the same block of codes which we are going use two or more locations in a program. This is especially useful if the code involved is long or complicated ****************************************************************** Q.66: What is the disadvantage of using function? Ans: CPU time is wasted : Function involves branching , CPU time is wasted due to branching . Sequential execution of statements is fast . Function call is slow in execution. Don't call same function too many times, call the function only once store the result in a variable , use variable subsequently. Minimise number function calls in a program. ****************************************************************** Q.67: What is Pointers? Ans: The use of pointers in C language allows one to keep the hold of the variable or the literal's address. This address can then be passed on from a function to the other function. This whole process is done without the help of a multiple variable declaration. There are many OS that use pointers very extensively. If you know the address of the variable or the literal, then they can be called easily as the parameters. This particular feature is what makes that C and C++ to stand out of most of the programming languages. The reason between these languages standing out of the other languages is that they usually bridge between the low level and the high levels of programming. ******************************************************************

## 61|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) Q.68 What is Preprocessor directives? Ans: Preprocessor directives are lines included in the code of our programs that are not program statements but directives for the preprocessor. These lines are always preceded by a hash sign (#). The preprocessor is executed before the actual compilation of code begins, therefore the preprocessor digests all these directives before any code is generated by the statements.These preprocessor directives extend only across a single line of code. As soon as a newline character is found, the preprocessor directive is considered to end. No semicolon (;) is expected at the end of a preprocessor directive. The only way a preprocessor directive can extend through more than one line is by preceding the newline character at the end of the line by a backslash (\). macro definitions (#define, #undef) To define preprocessor macros we can use #define. Its format is: #define identifier replacement ****************************************************************** Q.. 69 What is Dynamic Memory Allocation? Ans: The process of allocating memory at run time is known as dynamic memory allocation. Although C does not inherently have this facility there are four library routines which allow this function. Many languages permit a programmer to specify an array size at run time. Such languages have the ability to calculate and assign during executions, the memory space required by the variables in the program. But c inherently does not have this facility but supports with memory management functions, which can be used to allocate and free memory during the program execution. The following functions are used in c for purpose of memory management. Function Task malloc Allocates memory requests size of bytes and returns a pointer to the Ist byte of allocated space calloc Allocates space for an array of elements initializes them to zero and returns a pointer to the memory free Frees previously allocated space realloc Modifies the size of previously allocated space. ******************************************************************

## 62|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) Q. 70. What is string? Explain the concept of string using suitable example? Ans: String is an array of characters. Array is an collection of similar data type stored in contiguous memory locations. Each location of string store single character which require 1 byte of memory. Last character of string is always \0 which is known as null character or end of string character. During input and output operation with string, function always performs its operation till the end of character i.e. \0. Example: Char str[10]={krishna}; k r i s h n a \0 ? ? In the above example, str is a string variable containing 10 location. Each location require 1 byte of memory therefore total memory allocated by str are 10 bytes. Staring address of string is Known as base address, base address is always present in variable name i.e. str is a variable name Containing base address. By using base address we can access characters of string. Str stores base Address of first character, str+1 stores base address of second character and so on. Also we can Access character of string by using variable name and index number as given below. str[0]= k str[1]= r str[2]= i str[3]= s str[4]= h str[5]= n str[6]= a str[7]= \0 Program main() { Char str[]={krishna}; Clrscr(); printf(\n%c%c,str[0],str[3]); printf(\n%s%s,str,str+2); getch() } output:
63|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) ks krishna ishna ****************************************************************** Q.71. Explain scanf(),gets()and puts() functions? Ans: Scanf() is a input function which is used to scan integer number, character, float and string From the user. Scanf() function always terminates with spacebar or enter key therefore we cannot Scan string containing space from the user. Format specified %d,%c,%f and %s is used to scan integer, character, float and string respectively. Gets() is a input function which is used to scan only string from the user. Gets() function always terminates with enter key therefore this function always used to scan string containing Space. Puts() is a output function which is used to display string on the output screen. Puts() function always terminates with \0 character therefore this function display all character of string till end of character (\0). Example1: void main() { char str[30]; clrscr(); printf( Enter your name:); scanf( %s,str); puts(str); getch(); } Output: Enter your name: Praveen Sen Praveen Example2: main() { char str[30]; clrscr(); printf( Enter your name:);

## 64|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) gets(str); puts(str); getch(); } ****************************************************************** Q.72. Explain getch() and getche() function? Ans: getch gets a character from console but does not echo not to the screen getche gets a character from console, and echoes to the screen Declaration: 1. int getch(void); 2. int getche(void); Remarks: getch reads a single character directly from the keyboard, without echoing To the screen. getche reads a single character from the keyboard and echoes it to the current text window. return value: Both function return the character read from the keyboard. ******************************************************************

Q.73. Explain fgetc() and fputc() function? Ans: Fgetc gets a character from a stream Fputc outputs a character to a stream Declaration: 1. int fgetc(FILE*stream); 2. int fputc(int c, FILE* stream); Remarks : fgetc returns the next character on the named input stream. It is a function Version of the getc macro. fputc outputs character c to the named stream. Return Value: On success, fgetc returns the character read, after converting it to an int Without sign extension.

## 65|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) fputc returns the character c. On end-of-file or error, fgetc returns EOF On error, fputc returns EOF ****************************************************************** Q.74. Difference between getchar() & putchar()? Ans: 1. getchar() is a input function, which is used to input character from standard input device(keyboard). Syntax: int getchar(); getchar() return ascii number of character input by the user. 2.putchar() is a output function, which is used to output character on the output screen. Syntax: putchar(int ch); Example: main() { char ch; clrscr(); printf( Enter any character:) ch=getchar(); printf( your character=%c, putchar(ch); getch(); } output: Enter any character:\$ your character=\$ ****************************************************************** Q.75. Write down the various function used in FILE? Ans. C supports a number of functions that have the ability to perform basic file operations, which include: 1. Naming a file 2. Opening a file 3. Reading from a file

## 66|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) 4. Writing data into a file 5. Closing a file Defining and opening a file: If we want to store data in a file into the secondary memory, we must specify certain things about the file to the operating system. They include the fielname, data structure, purpose. The general format of the function used for opening a file is FILE *fp; fp=fopen(filename,mode); The first statement declares the variable fp as a pointer to the data type FILE. As stated earlier, File is a structure that is defined in the I/O Library. The second statement opens the file named filename and assigns an identifier to the FILE type pointer fp. This pointer, which contains all the information about the file, is subsequently used as a communication link between the system and the program. The second statement also specifies the purpose of opening the file. The mode does this job.

****************************************************************** Q.76. Write down the difference between structure and union? Ans: Structure Union 1. At a time all fields are active. 1. At a time only one field is active. 2. The total size of the structure 2. Total size of union is the size of is the sum of the size of the largest field in it. individual fields (member variable) 3. Member variables are allocated 3. Member variables access the same memory different memory locations. Location. ****************************************************************** Ques 77. What are the file opening modes available in C? Ans: Following are the various file opening modes. a opens existing file for appending. r opens existing file for reading operation.

## 67|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) w opens a new file for writing. r+ opens existing file for reading and writing operation. a+ opens existing file for reading and appending operation. ****************************************************************** Q.78. Explain static and dynamic memory allocation? Ans: When operating system runs multiple programs in memory then it will allocates different memory locations for different programs. If memory requirement of each program is already fixed Before the execution of program then it is easier job for operating system to allocate memory such Allocation is known as static memory allocation. When operating system runs multiple programs in memory then it will allocates different memory locations for different program. If memory requirement of each program is not fixed before the execution of program then operating system will allocates memory, such allocation is known as dynamic memory allocation ****************************************************************** Q.79.Write difference between syntax & semantic error. Ans: 1) Syntax refers to formal rules governing the construction of valid statements in a language. Semantics refers to the set of rules which give the meaning of a statement. 2) Errors due to syntax occur in a program when ruels of the programming language are violated or misused. Errors due to semantics occur in a program when statements are not meaningful. 3) Word order is the basic principal of syntax, those trying to understand what is written use the syntactic cues of word order to help give the sentence structure and meaning. Semantics are an individuals own interpretation of the meaning of a "sentence" based on their prior knowledge. Therefore a sentence that seemingly makes no syntactic sense, can have meaning when using semantic cues. 4) Syntax is only concerned with what is linguistically and grammatically correct. Semantics requires all ones prior knowledge which, and is far beyond anything

## 68|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) which is language specific. ****************************************************************** Q.80. Explain explicity type casting. Ans: Typecasting involves converting an expression of a given type into another type. In C, you can perform typecasting by placing the datatype name in parentheses and placing this in front of the value. For example: int main() { int x; x = (int)8 / 6; return 0; } In the above example, the usual result of 8/6 should be 1.3333 but since we have typecasted it, the x value would be "1". The later part is simply dropped. Typecasting is very essential in programming to avoid any unnecessary errors or warnings. Sometimes C compilers need you to define as to what type of typecasting is needed whereas on the other it implicitly converts. The usual notification is "(<type>)<value>". Typecasting is like you want to make a variable of one type, such as an int, to act like another type, such as char, for single statement. Although typecast has so many benefits but still it is considered as a headache because it simply truncates your data at important steps. ****************************************************************** Q.81.Whatisqualifiers.Explaintypesofqualifier. Ans: A type qualifier is used to refine the declaration of a variable, a function, and parameters, by specifying whether:

The value of an object can be changed The value of an object must always be read from memory rather than from a register More than one pointer can access a modifiable memory address

## 69|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) 1) Volatile: A variable should be declared volatile whenever its value could change unexpectedly. In practice, only three types of variables could change: ? Memory-mapped peripheral registers ? Global variables modified by an interrupt service routine ? Global variables within a multi-threaded application 2) Constant: The addition of a 'const' qualifier indicates that the (relevant part of the) program may not modify the variable. ****************************************************************** Q82.Explain loops used in c . Ans: To execute a set of instructions repeatedly until a particular condition is being satisfied. Three types of looping statements are there 1) 2) 3) For Loop While Loop Do while Loop

For Loop:In for looping statement allows a number of lines represent until the condition is satisfied Syntax: for(initialize counter variable ; condition ; increment/decrement the counter variable) { Statement1; ... Statement n;

## 70|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) }

Example program using for loop: main() { int counter,a[10]; for(counter=0;counter<10;counter++) { printf(Enter a[%d]=,counter); scanf(%d\n,a[counter]); } printf(Array elements:); for(counter=0;counter<10;counter++) printf(%d,a[counter]); } Explanation: In above program array a length 10.It occupies 20bytes in memory. Counter is normal integer variable. It is used to read and display the array values. First FOR LOOP is used to read the array values. Second FOR LOOP is used to display the array values. In this second FOR LOOP using one line. So, no need of brace brackets WHILE LOOP: In While looping statement allows a number of lines represent until the condition is satisfied Syntax: while( condition)
71|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) { Statement1; ... Statement n; } Example program using while loop: main() { int counter,a[10]; counter=0; while(counter<10) { printf(Enter a[%d]=,counter); scanf(%d\n,a[counter]); counter+=1; } printf(Array elements:); counter=0; while(counter<10) { printf(%d,a[counter]); counter+=1; } } Explanation: In above program array a length 10.It occupies 20bytes in memory.
72|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) Counter is normal integer variable. It is used to read and display the array values. Before using counter variable to initialize counter. Then use it in WHILE LOOP. First WHILE LOOP is used to read the array values. Second WHILE LOOP is used to display the array values. DO WHILE LOOP: In DO WHILE LOOP first execute the statements then it checks the condition. Syntax: do { Statement1; ... Statement n; }while(condition); Example program using for loop: main() { int counter; counter=0; do { printf(%d,counter); }while(counter>0); ****************************************************************** Q.83.Write a user define function reverse which will reverse from the value pass from the function name() using pointers.

## 73|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) # include <stdio.h> # include <conio.h> void reverse (int *) void main() { int num; clrscr(); printf(\n enter the number); scanf(%d,&num); reverse(&num); printf(\n reverse =%d, num); getch(); } void reverse(int *p) { int d,rev=0; while(*p>0) { d=*p%10; rev=rev*10+d; *p=*p/10; } *p=rev; } ****************************************************************** Q.84Explain the various standard library function of strings used in c. Ans:

## 74|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) This discussion of string handling in C presumes that the following compiler directive is used. #include <string.h> On Linux, this file is located in /usr/include. Only the basic functions will be discussed here, so you may want to investigate string.h. In C, a string is stored as a null-terminated char array. This means that after the last truly usable char there is a null, hex 00, which is represented in C by '\0'. The subscripts used for the array start with zero (0). The following line declares a char array called str. C provides fifteen consecutive bytes of memory. N.B. Only the first fourteen bytes are usable for character storage, because one must be used for the string-terminating null. char str[15]; The following is a representation of what would be in RAM, if the string "Hello, world!" is stored in this array. Characters: H e l l o , w o r l d ! Hex values: 48 65 6C 6C 6F 2C 20 77 6F 71 6C 64 21 00 Subscripts: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 The name of the array is treated as a pointer to the array. The subscript serves as the offset into the array, i.e., the number of bytes from the starting memory location of the array. Thus, both of the following will save the address of the 0th character in the pointer variable ptr. ptr = str; ptr = &str[0]; strlen() Syntax: len = strlen(ptr); where len is an integer and ptr is a pointer to char

## 75|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) strlen() returns the length of a string, excluding the null. The following code will result in len having the value 13. int len; char str[15]; strcpy(str, "Hello, world!"); len = strlen(str);

strcpy() Syntax: strcpy(ptr1, ptr2); where ptr1 and ptr2 are pointers to char strcpy() is used to copy a null-terminated string into a variable. Given the following declarations, several things are possible. char S[25]; char D[25];

Putting text into a string: strcpy(S, "This is String 1."); Copying a whole string from S to D: strcpy(D, S); Copying the tail end of string S to D: strcpy(D, &S[8]);

strncpy() Syntax: strncpy(ptr1, ptr2, n); where n is an integer and ptr1 and ptr2 are pointers to char strncpy() is used to copy a portion of a possibly null-terminated string into a variable. Care must be taken because the '\0' is put at the end of destination string only if it is within the part of the string being copied. Given the following declarations, several things are possible. char S[25];

## 76|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) char D[25]; Assume that the following statement has been executed before each of the remaining code fragments.

Putting text into the source string: strcpy(S, "This is String 1."); Copying four characters from the beginning of S to D and placing a null at the end: strncpy(D, S, 4); D[4] = '\0'; Copying two characters from the middle of string S to D: strncpy(D, &S[5], 2); D[2] = '\0'; Copying the tail end of string S to D: strncpy(D, &S[8], 15); which produces the same result as strcpy(D, &S[8]);

strcat() Syntax: strcat(ptr1, ptr2); where ptr1 and ptr2 are pointers to char strcat() is used to concatenate a null-terminated string to end of another string variable. This is equivalent to pasting one string onto the end of another, overwriting the null terminator. There is only one common use for strcat(). char S[25] = "world!"; char D[25] = "Hello, ";

## Concatenating the whole string S onto D: strcat(D, S);

strncat() Syntax: strncat(ptr1, ptr2, n); where n is an integer and ptr1 and ptr2 are pointers to char
77|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) strncat() is used to concatenate a portion of a possibly null-terminated string onto the end of another string variable. Care must be taken because some earlier implementations of C do not append the '\0' at the end of destination string. Given the following declarations, several things are possible, but only one is commonly used. char S[25] = "world!"; char D[25] = "Hello, ";

Concatenating five characters from the beginning of S onto the end of D and placing a null at the end: strncat(D, S, 5); strncat(D, S, strlen(S) -1); Both would result in D containing "Hello, world".

strcmp() Syntax: diff = strcmp(ptr1, ptr2); where diff is an integer and ptr1 and ptr2 are pointers to char strcmp() is used to compare two strings. The strings are compared character by character starting at the characters pointed at by the two pointers. If the strings are identical, the integer value zero (0) is returned. As soon as a difference is found, the comparison is halted and if the ASCII value at the point of difference in the first string is less than that in the second (e.g. 'a' 0x61 vs. 'e' 0x65) a negative value is returned; otherwise, a positive value is returned. Examine the following examples. char s1[25] = "pat"; char s2[25] = "pet"; diff will have a negative value after the following statement is executed. diff = strcmp(s1, s2); diff will have a positive value after the following statement is executed.

## 78|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) diff = strcmp(s2, s1); diff will have a value of zero (0) after the execution of the following statement, which compares s1 with itself. diff = strcmp(s1, s1);

strncmp() Syntax: diff = strncmp(ptr1, ptr2, n); where diff and n are integers ptr1 and ptr2 are pointers to char strncmp() is used to compare the first n characters of two strings. The strings are compared character by character starting at the characters pointed at by the two pointers. If the first n strings are identical, the integer value zero (0) is returned. As soon as a difference is found, the comparison is halted and if the ASCII value at the point of difference in the first string is less than that in the second (e.g. 'a' 0x61 vs. 'e' 0x65) a negative value is returned; otherwise, a positive value is returned. Examine the following examples. char s1[25] = "pat"; char s2[25] = "pet"; diff will have a negative value after the following statement is executed. diff = strncmp(s1, s2, 2); diff will have a positive value after the following statement is executed. diff = strncmp(s2, s1, 3); diff will have a value of zero (0) after the following statement. diff = strncmp(s1, s2, 1); ******************************************************************

## 79|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank)

Q.85.Explain the standard library functions used in files in C. Ans: File operation functions in C: Function Operation Name

fopen()

Creates a new file for use Opens a new existing file for use

Fclose

getc()

putc()

fprintf()

fscanf()

getw()

putw()

## 80|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank)

fseek()

ftell()

rewind()

## Sets the position to the begining of the file

Defining and opening a file: If we want to store data in a file into the secondary memory, we must specify certain things about the file to the operating system. They include the fielname, data structure, purpose.

## The general format of the function used for opening a file is

FILE fp=fopen(filename,mode);

*fp;

The first statement declares the variable fp as a pointer to the data type FILE. As stated earlier, File is a structure that is defined in the I/O Library. The second statement opens the file named filename and assigns an identifier to the FILE type pointer fp. This pointer, which contains all the information about the file, is subsequently used as a communication link between the system and the program. The second statement also specifies the purpose of opening the file. The mode does this job. R open the file for read only.

## 81|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) W A open open the file file for for appending writing data only. it.

the

to

## FILE p1=fopen(data,r); p2=fopen(results,w);

*p1,

*p2;

In these statements the p1 and p2 are created and assigned to open the files data and results respectively the file data is opened for reading and result is opened for writing. In case the results file already exists, its contents are deleted and the files are opened as a new file. If data file does not exist error will occur.

Closing a file: The input output library supports the function to close a file; it is in the following format. fclose(file_pointer);

A file must be closed as soon as all operations on it have been completed. This would close the file associated with the file pointer. Observe the following program.

*p1

## IIISemETC(C&DatastructureQuestionBank) fclose(p1); fclose(p2)

The above program opens two files and closes them after all operations on them are completed, once a file is closed its file pointer can be reversed on other file. The getc and putc functions are analogous to getchar and putchar functions and handle one character at a time. The putc function writes the character contained in character variable c to the file associated with the pointer fp1. ex putc(c,fp1); similarly getc function is used to read a character from a file that has been open in read mode. c=getc(fp2).

The getw and putw functions: These are integer-oriented functions. They are similar to get c and putc functions and are used to read and write integer values. These functions would be usefull when we deal with only integer data. The general forms of getw and putw are:

putw(integer,fp); getw(fp);

The fprintf & fscanf functions: The fprintf and fscanf functions are identical to printf and scanf functions except that they work on files. The first argument of theses functions is a file pointer which specifies the file to be used. The general form of fprintf is

## 83|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank)

Where fp id a file pointer associated with a file that has been opened for writing. The control string is file output specifications list may include variable, constant and string. fprintf(f1,%s%d%f,name,age,7.5); Here name is an array variable of type char and age is an int variable The general format of fscanf is fscanf(fp,controlstring,list); This statement would cause the reading of items in the control string. Example: fscanf(f2,5s%d,item,&quantity); Random access to files: Sometimes it is required to access only a particular part of the and not the complete file. This can be accomplished by using the following function: 1 > fseek fseek function: The general format of fseek function is a s follows: fseek(file pointer,offset, position); ****************************************************************** Q.86.Write a program to perform the multiplication of 3*3 matrix. #include <stdio.h> #include<conio.h> #include<math.h>

## 84|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank)

void main { int A[3][3],B[3][3],C[3][3]; clrscr(); printf(\n enter elements of matrix A); for(i=0;i<3;i++) { for(i=0;i<3;i++) { scanf(%d,&A[i][j]); } } printf(\n enter elements of matrix B); for(i=0;i<3;i++) { for(i=0;i<3;i++) { scanf(%d,&B[i][j]); } } for(i=0;i<3;i++) { for(i=0;i<3;i++) { sum=0; for(k=0;k<3;k++) { sum=sum+A[i][k]*B[k][j]; } C[i][j]=sum; }
85|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) } printf(\n Matrix A); for(i=0;i<3;i++) { for(i=0;i<3;i++) { scanf(%d,&A[i][j]); } printf(\n); } printf(\n Matrix B); for(i=0;i<3;i++) { for(i=0;i<3;i++) { scanf(%d,&B[i][j]); } printf(\n); } printf(\n Resultant matrix); for(i=0;i<3;i++) { for(i=0;i<3;i++) { scanf(%d,&C[i][j]); } printf(\n); } getch(); } ******************************************************************

## 86|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank)

Q.87.Write a switch block to the following if next ch =+ invoke fuc add(a,b); if next ch =- invoke fuc sub(a,b); if next ch =* invoke fuc mul(a,b); if next ch =/ invoke fuc div(a,b); else exit from switch block. #include <stdio.h> #include<conio.h> #include<math.h> void add(int,int); void sub(int,int); void mul(int,int); void div(float, float); void main() { char ch; int a,b; float p,q; clrscr(); printf(Addition); printf(Subtraction); printf(Multiplication); printf(Division); printf(Enter choice); scanf(%c,&ch); switch(ch) case +: printf(Enter the value of a& b); scanf(%d,%d,&a,&b); add(a,b);
87|P a g e Nagpur Institute Of Technology, Nagpur (MS)

1) 2) 3) 4)

IIISemETC(C&DatastructureQuestionBank) break; case -: printf(Enter the value of a& b); scanf(%d,%d,&a,&b); sub(a,b); break; case *: printf(Enter the value of a& b); scanf(%d,%d,&a,&b); mul(a,b); break; case /: printf(Enter the value of p& q); scanf(%f,%f,&p,&q); div(p,q); break; default:exit(0); } getch(); void add (int m, int n) { printf(Addition=%d,m+n); } void sub (int m, int n) { printf(Subtraction=%d,m-n); } void mul (int m, int n) { printf(Multiplication=%d,m*n); } void div(float m, float n) { printf(Division=%f,m/n); }
88|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) ****************************************************************** Q.88.Explain the basic structure of C program. Ans: The structure of a C program is a protocol (rules) to the programmer, while writing a C program. The general basic structure of C program is shown in the figure below. The whole program is controlled within main ( ) along with left brace denoted by { and right braces denoted by }. If you need to declare local variables and executable program structures are enclosed within { and } is called the body of the main function. The main ( ) function can be preceded by documentation, preprocessor statements and global declarations.

Documentations The documentation section consist of a set of comment lines giving the name of the program, the another name and other details, which the programmer would like to use later. Preprocessor Statements The preprocessor statement begin with # symbol and are also called the preprocessor directive. These statements instruct the compiler to include C preprocessors such as header files and symbolic constants before compiling the C program. Some of the preprocessor statements are listed below.

## 89|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank)

Global Declarations The variables are declared before the main ( ) function as well as user defined functions are called global variables. These global variables can be accessed by all the user defined functions including main ( ) function. The main ( ) function Each and Every C program should contain only one main ( ). The C program execution starts with main ( ) function. No C program is executed without the main function. The main ( ) function should be written in small (lowercase) letters and it should not be terminated by semicolon. Main ( ) executes user defined program statements, library functions and user defined functions and all these statements should be enclosed within left and right braces. Braces Every C program should have a pair of curly braces ({, }). The left braces indicates the beginning of the main ( ) function and the right braces indicates the end of the main ( ) function. These braces can also be used to indicate the user-defined functions beginning and ending. These two braces can also be used in compound statements. Local Declarations The variable declaration is a part of C program and all the variables are used in main ( ) function should be declared in the local declaration section is called local variables. Not only variables, we can also declare arrays, functions, pointers etc. These variables can also be initialized with basic data types. For examples. Code:
90|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank)

Main ( ) { int sum = 0; int x; float y; } Here, the variable sum is declared as integer variable and it is initialized to zero. Other variables declared as int and float and these variables inside any function are called local variables. Program statements These statements are building blocks of a program. They represent instructions to the computer to perform a specific task (operations). An instruction may contain an input-output statements, arithmetic statements, control statements, simple assignment statements and any other statements and it also includes comments that are enclosed within /* and */ . The comment statements are not compiled and executed and each executable statement should be terminated with semicolon. User defined functions These are subprograms, generally, a subprogram is a function and these functions are written by the user are called user ; defined functions. These functions are performed by user specific tasks and this also contains set of program statements. They may be written before or after a main () function and called within main () function. This is an optional to the programmer. Now, let us write a small program to display some message shown below. Code: # include <stdio.h> main() { printf ("welcome to the world of C/n"); }

******************************************************************

## IIISemETC(C&DatastructureQuestionBank) Q.89.Explain the unformatted console functions used in C. Ans:

The functions under this category are : getch() putch() gets() getche() putchar() puts() getchar() fputchar() fgetchar()

These above listed function broadly fall into two categories, the one which deal with a single character and the second which deal with a string of characters. Explanations : getch() : This function will read a single character the instant it is typed by programmer without waiting for the Enter Key to be hit. The typed character is not echoed on screen. getche() : This function will also read a single character the instant it is typed by programmer without waiting for the Enter key to be hit, just like getch() function. The additional character 'e' in function getch() echoes the character on screen that you typed. This is a point of differences between getch() & getche(). getchar() : It works similar to that of getch() and also echoes the character you typed on the screen but it also requires enter key to be hit immediately after the character that you typed. It is a macro. fgetchar() : It will echo the character on screen and it requires hit of Enter key immediately following the character. The only difference between getchar() and fgetchar() is that the former is a macro and latter is function. putch() : It writes a character to the screen. putchar() : It writes a character to the screen and is a macro. fputchar() : It writes a character to the screen (function version).

## 92|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) (Note : putch(), putchar() and fputchar() can output only one character at a time on screen.) gets() : It gets a string from the keyboard and it is necessary on programmers part to terminates that string with an Enter key. That's why spaces and tabs are acceptable as a part of the input string if any gets() is used to read only one string at a time. puts() : It outputs a string to the screen puts() can outputs only one string at a time. ****************************************************************** Q.90.Explain the operator precedence in C. Ans: This page lists C operators in order of precedence (highest to lowest). Their associativity indicates in what order operators of equal precedence in an expression are applied. Operator Description Associativity left-to-right Parentheses (function call) (see Note 1) () Brackets (array subscript) [] Member selection via object name . Member selection via pointer -> Postfix increment/decrement (see Note 2) ++ -right-to-left Prefix increment/decrement ++ -Unary plus/minus + Logical negation/bitwise complement ! ~ Cast (change type) (type) Dereference * Address & Determine size in bytes sizeof * / % Multiplication/division/modulus left-to-right + Addition/subtraction left-to-right << >> Bitwise shift left, Bitwise shift right left-to-right < <= Relational less than/less than or equal to left-to-right > >= Relational greater than/greater than or equal to == != Relational is equal to/is not equal to left-to-right & Bitwise AND left-to-right

## 93|P a g e Nagpur Institute Of Technology, Nagpur (MS)

IIISemETC(C&DatastructureQuestionBank) ^ Bitwise exclusive OR left-to-right | Bitwise inclusive OR left-to-right && Logical AND left-to-right || Logical OR left-to-right ?: Ternary conditional right-to-left right-to-left Assignment = Addition/subtraction assignment += -= Multiplication/division assignment *= /= Modulus/bitwise AND assignment %= &= Bitwise exclusive/inclusive OR assignment ^= |= Bitwise shift left/right assignment <<= >>= , Comma (separate expressions) left-to-right ******************************************************************

Acknowledgements:
We are thankful to all the authors of Text Books of Subject: C & DATA STRUCTURE that are cited during the compilation of this Question Bank to provide to the students. Questions, answers and related information are collectively compiled and presented in single form based on the material cited from various contributions from the authors and online sources referred below. References: www.wikipedia.org Let us C by Yashwant Kanetkar ANSI C by Balaguruswamy