You are on page 1of 15

# REVIEW QUESTIONS Question 01 What will be the output once the following piece of code is run?

for (x=1, y=5; x+y<=1 ; x++! " printf(#\$i\$i%, x,y!; y++; & OUTPUT 152637 Question 02 What will be the output of the following progra' 'ain( ! " int (, nu'=) ; (=(nu'*5 ? (nu'<= 1 ? 1 +, !+5 !; printf(#-n\$d%, nu'!; & OUTPUT 30 Question 03 a. What is the output of the following piece of codes int n = , '=1; do " printf(#\$d%, '!; '++; & while('<=n!; OUTPUT 1 b. int n= , '; for ('=1; '<=n+1; '++! printf(#\$d%, '!; OUTPUT 1

c. /ow 'any ti'es the following loop be executed " 0 ch = 1b2; while(ch *= 1a2 33 ch <= 142! ch++; & OUTPUT 25 Question 04 Write a 5 progra' that uses 1for2 construct to find the su' of the following har'onic series for a gi6en 6alue of n and display the su'. 1 + 17, +17) +0 + 17n 89:W;<+
#include<stdio.h> #include<conio.h> void main() { double n,sum=0,i; clrscr(); printf(" n !lease "ive #he \$alue of %& "); scanf("'lf",(n); for(i=);i<=n;i**) { sum = sum * ()+i); if(i==)) printf(" n ) *"); elseif(i==n) printf(" ()+'d) ",i); else printf(" ()+'d) * ",i); , printf(" n n #-. /01 23 #-4/ /.54./ 4/ '.6lf",sum); 7etch(); ,

) * ()+6) *

()+:) *

()+;) *

()+9)

## #-. /01 23 #-4/ /.54./ 4/ 6.6<

Question 05 a. Write a 5 progra' frag'ent using #do0while% construct to print out e6en nu'bers between 1 to 1 'a(ing sure that two nu'bers are written per line b. :hown below is a =loyd2s triangle. Write a 5 progra' to print this triangle

a.Ans: A C program to print even numbers between 10 to 100 using do-while construct: #include <stdio.h> main(){ int i,k; k=10; i=0; do { if(k>10) { printf("%d",k); printf(" "); if(i%2==0) printf("\n"); } i++; k=k+2; }while(k<100); getch(); } b. Ans: A C program to print Floyds triangle up to 1 is given below: #include <stdio.h> main() { int num=1,k=1,j; while(num<=91) {

for(j=1;j<=k;j++) printf(" %d",num++); printf("\n\n"); k++; } getch(); } Question 06 a. >efine a structure na'ed 1student2 containing two fields 1na'e2 and 1'ar(s2. Ans: A structure student: struct student { String name[]; float marks[]; }; Or Struct student{ Char name[20]; Float marks; }; b. >eclare an array of structure ha6ing 5 ele'ents of student type. Ans: An array o! structure: struct student stu"#0\$% c. Write an input state'ent for inputting the 'ar(s and the na'es of 5 students defined as abo6e. Ans: &tatements !or inputting mar's o! #0 students: for(i=0;i<50;i++) { printf("%d. name : ",i+1); scanf("%s",&stu[i].name); printf("\nenter marks :"); scanf("%d",&stu[i].marks); printf("\n"); } d. Write a co'plete 5 progra' to co'pute and print the na'es of those students who ha6e got 'ore than ? 'ar(s. 8lso print their 'ar(s along with their na'es. Ans: A C program to compute and print names o! students who scored more than (0 mar's #include<stdio.h> #include<conio.h> struct student { char name[10]; int marks; }; void main() {

int i,n; struct student stu[50]; clrscr(); printf("enter no. of students"); scanf("%d",&n); printf("ENTER NAME AND MARKS\n"); for(i=0;i<n;i++) { printf("%d. name : ",i+1); scanf("%s",&stu[i].name); printf("\nenter marks :"); scanf("%d",&stu[i].marks); printf("\n"); } for(i=0;i<n;i++) { if(stu[i].marks>80) { printf("%s",stu[i].name); printf("\t"); printf("%d",stu[i].marks); printf("\n"); }} getch(); } Question 07 a. Write a function 1exchange2 to interchange the 6alues of two 6ariables say x and y. @llustrate the use of this function in calling progra'. 8ssu'e that x and y are defined as global 6ariables. Ans: A C !unction e)change to interchange the values o! ) and y: #include<stdio.h> #include<conio.h> #include<math.h> int i,j; int x,y; void main() { i=10,j=20; clrscr(); printf("The values before exchange is i: %d, j:%d\n",i,j); exchange(); printf("The values after exchange is i: %d, j:%d\n",i,j); printf("\n");

getch(); } void exchange() { int temp; temp = x; x = y; y = temp; } b. :uppose a 'e'ber of a structure is a pointer 6ariable. /ow can the obAect of the pointer be accessed in ter's of structure 6ariable na'e and the 'e'ber na'e? Ans: struct rec { int *a;}obj; to use pointer member with structure variable !ollowing operator is used* ob+-,a% that is ordinary variables are used with dot -*. operator and pointer variables are used with arrow --,. operator* Question 08 a. >istinguish between the following+ (i! :yntactic error and se'antic error (ii! <un ti'e error and logical error (iii! 5o'piler and @nterpreter Ans: (i)Syntactic error and semantic error Syntactic errors also 'nown as compilation errors are caused by violation o! the grammar rules o! the language* /he compiler detects0 isolate these errors and give terminate the source program a!ter listing the errors* &ome o! the common syntactic errors are: missing or misplaced ; or } missing return type !or a procedure missing or duplicate variable declaration Semantic errors are logical errors* 1! there is a semantic error in a program0 it will run success!ully0 in the sense that the computer will not generate any error messages0 but it will not do the right thing* /he problem is that the meaning o! the program -its semantics. is wrong* 1denti!ying semantic errors can be tric'y because it re2uires wor'ing bac'ward by loo'ing at the output o! the program and trying to !igure out what it is doing* (ii) Run time error and logical error

Run-time errors: 3rrors such as mismatch o! data types or array out o! bound error are 'nown as runtime errors* /hese errors are generally go undetected by the compiler so programs with run-time error will run but produce erroneous results* Logical errors: /hese are the errors related with the logic o! the program e)ecution* /hese errors are not detected by the compiler and are primarily due to a poor understanding o! the problem or a lac' o! clarity o! hierarchy o! operators* &uch errors cause incorrect result* (iii) Compiler and Interpreter /hese are two types o! language translators* A compiler converts the source program -user-written program. into an ob+ect code -machine language by chec'ing the entire program be!ore e)ecution* 1! the program is error !ree0 ob+ect program is created and loaded into memory !or e)ecution* A compiler produces an error list o! the program in one go and all have to be ta'en care even be!ore the e)ecution o! !irst statement begin* 1t ta'es less time !or e)ecution* An interpreter is also a language translator that translates and e)ecutes statements in the program one by one* 1t wor' on one statement at a time and i! error !ree0 e)ecutes the instruction be!ore going to second instruction* 4ebugging is simpler in interpreter as it is done in stages* An interpreter ta'es more time !or e)ecution o! a program as compared to a compiler* b. >esign an algorith' that accepts a positi6e integer and re6erses the order of its digits. Ans: An algorithm to !ind reverse o! an integer is: void reversein() { int n,r; printf("enter an integer"); scanf("%d",&n); printf("\nreverse of %d : ",n); while(n>0) { r=n%10; printf("%d",r); n=n/10; } }

Question 09 Write a function, which ta(es an array of real nu'bers and its si4e as argu'ents and returns the 'axi'u'. Bsing the abo6e function write a progra' to read a set of real nu'bers fro' the (eyboard and find the 'axi'u' nu'ber in the array. Ans: A C program to read a set o! real numbers !rom the 'eyboard and !ind ma)imum among them using a ma) !unction: #include<stdio.h> #include<conio.h> void main() { int i,n; float a[100]; clrscr(); printf("\nhow many elements u want to enter :\n"); scanf("%d",&n); printf("\nEnter the elements:"); for(i=0;i<n;i++) scanf("%f",&a[i]); max(a,n); getch(); } max(float a[],int n) { int i; float k,large; large=a[0]; for(i=1;i<n;i++) { if(a[i]>large) { k=a[i]; a[i]=large; large=k; } } printf("largests element is : %f",large); } Question 10 a. ;xplain the following state'ents+ (i! getchar(! (ii! putchar(! Ans: (i) getchar( ): /he !unction getchar-. obtains the ne)t character !rom the stream stdin* 1t provides bu!!ered character input with echo0 and its prototype is int getchar-void.%

(ii) putchar( ): /he putchar-. Function located in stdio*h0 is as !ollows: int putchar-int c.% /his !unction writes the character stored in c to stdout* Although the prototype speci!ies a type int argument0 we pass putchar-. a type char* /he !unction returns the character that was +ust written0 or 35F i! an error has occurred* A program to demonstrate getchar-. and putchar-. is given below along with output and e)planation: #include <stdio.h> main(){ int ch; while ((ch = getchar()) != `\n') putchar(ch);} /oday is good weather /oday is good weather 6hen getchar-. !unction is called0 it waits to receive a character !rom stdin* 7ecause getchar-. is a bu!!ered input !unction0 no characters are received until 3nter is pressed* 8owever0 each pressed 'ey is echoed immediately on the screen* 6hen we press 3nter0 all the entered characters 0 including the newline0 are sent to stdin by the operating system* /he getchar-. !unction returns the characters one at a time0 assigning each in turn to ch* 3ach character is compared to the newline character 9:n and0 i! not e2ual0 displayed onscreen with putchar-.*6hen a newline is returned by getchar-.0 the while loop terminates* b. Write a progra' to find the su' 3 a6erage of the gi6en nu'bers using the doCCCCwhile loop. Ans: A C program to !ind the sum and average o! given numbers using dowhile loop is: #include<stdio.h> #include<conio.h> #include<math.h> void main() { int i=0,n; float sum,avg,num; clrscr(); sum=0; printf("How many numbers you want to find sum\n");

scanf("%d",&n); printf("Enter the numbers\n"); do{ scanf("%f",&num); sum=sum+num; i++;}while(i<n); avg=sum/n; printf("Sum=%f\n",sum); printf("Average=%f\n",avg); getch();} Question 11 a. What is a function? Dist out the ad6antages 3 disad6antages of using functions in 5? Ans: A function is a named0 independent section o! C code that per!orms a speci!ic tas' and optionally returns a value to the calling program* A !unction is named0 each have a uni2ue name* 7y using that name in another part o! the program0 one can e)ecute the statements contained in the !unction* Advantages o! using !unctions in C code: 1* A programmer may have a bloc' o! code that he has repeated !orty times throughout the program* A !unction to e)ecute that code would save a great deal o! space0 and it would also ma'e the program more readable* ;* 1t is easy to locate and isolate a !aulty !unction* 8aving only one copy o! the code ma'es it easier to ma'e changes* <* Another reason !or !unctions is to brea' down a comple) program into logical parts* For e)ample0 ta'e a menu program that runs comple) code when a menu choice is selected* /he program would probably best be served by ma'ing !unctions !or each o! the actual menu choices0 and then brea'ing down the comple) tas's into smaller0 more manageable tas's0 which could be in their own !unctions* 1n this way0 a program can be designed that ma'es sense when read* And has a structure that is easier to understand 2uic'ly* /he worst programs usually only have the re2uired !unction0 main0 and !ill it with pages o! +umbled code* =* A !unction may be used by many other programs* A programmer can use already compiled !unction instead o! starting over !rom scratch*

isadvantages o! using !unctions in C code: 6hen a program calls a !unction0 a certain amount o! processing overhead is re2uired in order to pass e)ecution to the !unction code and then return e)ecution to the calling program* 1! we are calling a small !unction again and again0 this processing overhead results in ine!!iciency* b. Write a progra' to read an integer nu'ber fro' the (eyboard, add 1 to it if the nu'ber read is e6en 3 again add 1 to it if the nu'ber is less than , . Etherwise, (eep the nu'ber unchanged. Ans: #include<stdio.h> #include<conio.h> void main() { int n; clrscr(); printf("enter a number"); scanf("%d",&n); if(n%2==0) { n=n+1; if(n<20) n=n+1; } printf("The Number is:%d",n); getch(); }

Question 12 a. Write a progra' to read a set of real nu'bers 3 find the 'axi'u' 3 the 'ini'u' nu'ber entered with the help of do.. while loop. Ans: #include<conio.h> float largest(float*,int); float smallest(float*,int); void main() { float a[20],k,l; int n,i,m; clrscr(); do { printf("how many numbers u want to enter"); scanf("%d",&n);

printf("enter numbers"); for(i=0;i<n;i++) scanf("%f",&a[i]); k=largest(a,n); l=smallest(a,n); printf("largest element = %f",k); printf("\nsmallest element = %f",l); printf("\npres 1 to perform again :"); scanf("%d",&m); }while(m==1); getch(); } float largest(float a[], int n) { float large,t; int i=1; large=a[0]; do { if(a[i]>large) { t=a[i]; a[i]= large; large=t; } i++; }while(i<n); return (large); } float smallest(float a[], int n) { float small,t; int i=1; small=a[0]; do { if(a[i]<small) { t=a[i]; a[i]=small; small=t; } i++; }while(i<n); return (small); } b. Write a 5 function using pointers to exchange the 6alues stored in two 'e'ory locations in the 'e'ory.

Ans: A C program to e)change the values stored in two memory locations: #include<stdio.h> #include<conio.h> #include<math.h> int i,j; void main(){ i=10,j=20;clrscr(); printf("The values before exchange is i: %d, j:%d\n",i,j); exchange(&i,&j); printf("The values after exchange is i: %d, j:%d\n",i,j); printf("\n"); getch();} exchange(int *x,int *y){ int temp; temp=*x; *x=*y; *y=temp; } Question 13 a. What do you 'ean by a loop? ;xplain the difference between the do loop, while loop, and for loop with the help of an exa'ple. Ans: >oop is a control structure used to per!orm repetitive operation* &ome programs involve repeating a set o! instruction either a speci!ied number o! times or until a particular condition is met* /his is done using a loop control structure* A program loop consists o! two parts: 7ody o! the loop and control statement* /he control statement tests certain conditions and then decides repeated e)ecution or termination o! statements* ?ost real programs contain some construct that loops within the program0 per!orming repetitive actions on a stream o! data or a region o! memory* /here are several ways to loop in C described below: !."hile statement: /he basic !ormat o! while statement is while (conditional expression) { ...block of statements to execute... } /he while loop continues to loop until the conditional expression becomes !alse* 5nce this e)pression become !alse0 the control is trans!erred out o! the loop* 5n e)it0 the program continues with the statement immediately a!ter the body o! the loop* For e)ample:

i=0; while(i<10) { printf(Hello world\n); i++; } /his statement will print 8ello world 10 times in a new line and come out o! the loop when 9i become 10* #. o statement: /his loop construct is o! the !orm: do { ...block of statements to execute... }while(conditional expression); 6hile construct chec's the conditional e)pression be!ore the loop is e)ecuted* &ometimes it is necessary to e)ecute the body o! the loop be!ore the conditional e)pression is evaluated* &uch situations are handled by do-while loop construct* 5n reaching the do statement0 the body o! the loop is evaluated and at the end o! the loop0 the conditional e)pression is chec'ed !or true or !alse* 1! true0it continues to evaluate the body again and when condition become !alse0 the control is trans!erred to the statement immediately a!ter the while statement*For e)ample: do { printf( Input a character\n); ch=getch( ); }while(ch=n); /his segment o! program reads a character !rom the 'eyboard until 9n is 'eyed in* \$. %or statement: /his is another entry-controlled loop having a general !orm: for (expression_1; expression_2; expression_3) { ...block of statements to execute... } /he e)pression@1 is !or initialiAation o! the control variable* /he condition is tested upon using the e)pression@;* 1! the condition is true0 the body o! the loop is e)ecuted* /hen control is trans!erred bac' to the !or loop e)pression@< where control variable is incremented using an assignment statement and new value o! variable is chec'ed through e)pression@; and process is repeated till the e)pression@; is evaluated to be !alse* 5n the termination o! loop0 the e)ecution continues with the statement immediately !ollowing the loop* /he for loop is a special case0 and is e2uivalent to the !ollowing while loop:

e)pression@1% while (expression_2) { ...block of statements... expression_3; } For instance, the statement in while lop takes the following form in the for loop: for (i = 1; i <10; i = i+1) { printf("Hello World\n"); } b. Write a progra' to swap two ele'ents using pointer concept. Ans:#include<stdio.h> #include<conio.h> #include<math.h> int i,j; void main( ) { i=10, j=20; clrscrO; printf("The values before swap is i: %d, j:%d\n",i,j); swap(&i,&j); printf("The values after swap is i: %d,j:%d\n",i,j); printf("\n"); getch( ); } swap(int *x,int *y) { int temp; temp=*x; *x=*y; *y=temp; }