Вы находитесь на странице: 1из 16

Unit-2 Questions and answers Sub:- COMPUTER CONCEPTS AND C PROGRAMMING What is the difference between formatted and

unformatted input/output statement. Ans:- formatted input and output state I/O (input-output) statements are classified into formatted and unformatted statements. In unformatted I/O statements no specification of data type and the manner in which they are read in or written out is mentioned. But, formatted I/O statements specify what type of data is being input or displayed and in what way it should be. Eg: For formatted I/O functions. Scanf() and printf() Eg: for unformatted I/O functions getchar(), putchar(), gets() and puts() etc., Q2. Explain getchar () function with example. Ans:- getchar As an example, here is a program to count the number of characters read until an EOF is encountered. EOF can be generated by typing Control - d. #include <stdio.h> main( ) { int ch, i = 0; while((ch = getchar()) != EOF) i ++; printf("%d\n", i); } Q3. Explain putchar () function with example. Ans: putchar putchar puts its character argument on the standard output (usually the screen). The following example program converts any typed input into capital letters. To do this it applies the function toupper from the character conversion library ctype.h to each character in turn. #include <ctype.h> /* For definition of toupper */ #include <stdio.h> /* For definition of getchar, putchar, EOF */ main( ) { int ch; while((ch = getchar()) != EOF) putchar(toupper(ch)) } Q1.

Q4 Explain Gets() and Puts() function Ans:- Gets() gets reads a whole line of input into a string until a newline or EOF is encountered. It is critical to ensure that the string is large enough to hold any expected input lines. When all input is finished, NULL as defined in stdio.h is returned. Puts() puts writes a string to the output, and follows it with a newline character. Example: Program which uses gets and puts to double space typed input. Q5 How do you classify Conditional Control Statement? Ans:- Conditional Statements are classified as follows: 1. if-statement 2. if-else statement 3. Nested-if statement 4. switch statement Q6. Write the syntax of if statement and if else statements? Ans:- if statement 2 way decision statement simple if statement if(expression) { statement block ; }

if .. else statement if(exp) { st block 1 ; } else { St block2; }

Q7. Write the syntax of nested statement? Ans:- nested if .. else statement if(cond1) { if(cond2) { st 1; } else

{ st 2; } } else { st 3; } Q8. Explain the syntax of else if ladder? Ans:- else if ladder: whenever else if followed by a condtion i.e each else statement is associated with if statement, then else if ladder is used. The general format of this statement is, if .. else .. if ladder if(cond1) st 1; else if(cond2) st 2; else if(cond3) st 3; . . else if(cond n) st n ; else default st; Q9. Explain the syntax of switch statement? Ans: switch Multiple branch selection statement Tests the value of an expression against a list of integer or char constants When a match is found, then statement associated with that constant is executed. switch(exp) { case const1 : st seq ; break ; case const2 : st seq ; break ; : : default : st seq ; } Q10. Explain the significance of goto statement in a C-programe?

Ans: goto statement is a simple statement used to transfer of the program control from one statement to the other statement unconditionally. The syntax of the goto statement is as follows: goto statement Unconditional branch goto label ; : : label : statement; where label is the name of the statement to which control is to be passed on. further it servers the purpose of identifying the statement. Rules to be followed for naming thelabels are same as those followed for naming variables, however labels need not be declared. Q11. Ans: What is a Loop in C language.` Looping is a powerful programming technique through which a group of statements is executed repeatedly, until certain specified condition is satisfied. Looping is also called a repetitive or an iterative control mechanism.

Q12. What is the difference between entry control loop and exit control loop? Ans: A program control loop consist of two segment, a) Body of the loop b) Control statement. Depending upon the position of the control statement of the loop control structure may be classified as entry control loop and exit control loop. Entry control loop: In the entry controlled loop the control conditions are tested before the start of the loop execution. If the conditions are not satisfied, then the body of the loop will not be executed. Exit control loop: In case of exit controlled loop, the test is performed at the end of the body of the loop and therefore the body is execufted unconditionally for the first time. Q13. List the different types of loop control structures? Ans: C provides three looping constructs for performing loop operations i) The while statement ii) The do statement iii) The for statement Q14. Explain the while statement with its syntax? With example, Ans: 1.The While loop:-While is a entry controlled loop statement, it tests the loop condition at the top of the loop, it the condition is true, then the body of the loop is executed, after execution of the body , it tests the condition again and it is true the loop executes once again, only when the condition is false the control is transferred out of the loop, and the program continues with the next statements the while loop keeps repeating an action until an associated test returns false. This is useful where the programmer does not know in advance how many times the loop will be traversed.

while (test condition) { body of the loop ; } For example: int x=3; main( ) { while (x>0) { printf("x=%d n",x); x--; } } outputs: x=3 x=2 x=1 ...to the screen. Q15. Explain the do while statement with its syntax? With example, Ans:. The do while statement called the co while loop checks its condition at the bottom of the loop, it is the do while loop always executes at least once, on reaching the do the program proceeds to evaluate the body of the loop , at the end of the loop the condition is checked it is true, the body of the loop is executed again, it is repeated till the condition is false, once it is false the control goes out of the while loop. The do while loops is similar, but the test occurs after the loop body is executed. This ensures that the loop body is run at least once.. do { body of the loop; } while ( test condition ); For example: int x=3; main( ) {

do { printf("x=%d n",x--); } while (x>0); } outputs: x=3 x=2 x=1 Q16. Explain the syntax of for loop? With example. Ans:- The for loop:- the for loop is another entry controlled loop , the general form of for loop is for ( initialization; test condition; increment/decrement) { body of the loop; } The initialization of the control variables is done first, The value of the control variable is tested using th test condition, if the condition is true the body of the loop is executed, otherwise the loop is terminated When the body of the loop is executed, the control is transferred back to the for statement after the last statement in the loop, now the control variable is incremented or decremented, such as I++ or I -- new value is again tested and the process repeats The for loop is frequently used, usually where the loop will be traversed a fixed number of times. It is very flexible, and novice programmers should take care not to abuse the power it offers. for syntax: for (<exp1>; <exp2>; <exp3>) statement; <exp1>: initialization, evaluated once. <exp2>: loop control, zero value means exit. <exp3>: counter, evaluated at end of body. Design issues: Expressions can be statements or statement sequences (separated by commas). No explicit loop var. Everything can be changed in the loop. Pretest. Example to calculate total of an array main( )

{ float total = 0.0; int i; for(i = 0; i < count; i++) total += array[i]; } NOTE: The postfix x-- operator which uses the current value of x while printing and then decrements x. Q17. Explain the break and continue statement? With example. Ans: break and continue C provides two commands to control how we loop: break -- exit form loop or switch. continue -- skip 1 iteration of loop. Consider the following example where we read in integer values and process them according to the following conditions. If the value we have read is negative, we wish to print an error message and abandon the loop. If the value read is great than 100, we wish to ignore it and continue to the next value in the data. If the value is zero, we wish to terminate the loop. while (scanf( ``%d'', &value ) == 1 && value != 0) { if (value < 0) { printf(``Illegal value n''); break; /* Abandon the loop */ } if (value > 100) { printf(``Invalid value n''); continue; /* Skip to start loop again */ } /* Process the value read */ /* guaranteed between 1 and 100 */ ....; ....; } /* end while value != 0 */ ----------xxxxxxxx-----------

EXAMINATION QUESTIONS AND ANSWERS

Q1.

Given an integer number write a C-program using while loop to reverse the digits of the number ? (10marks)

ANS:- /* Programme to reverse a given no */ # include <stdio.h> main() { int no, no1, rno=0,rem; printf ( \n enter an integer number ); scanf ( %d , &no); no1=no; while(no) { rem = no % 10; rno = rno*10 + rem; no/=10; } printf(the entered no is %d and the reversed number is %d,no1,no); } Q2. Write a program to find largest of three values using if else statement. (6marks) Ans:#include<stdio.h> main() { float a,b,c; printf("\n Enter three numbers "); scanf("%f%f%f",&a,&b,&c); if(a>b) if(a>c) printf("\n %f is the biggest",a); else printf("\n %f is the biggest",c); else if(b>c) printf("\n %f is the biggest",b); else printf("\n %f is the biggest",c); }

Q3. Write a program to find the LCM and GCD of two numbers. (8marks) Ans:#include<stdio.h> #include<math.h> main() { int m,n,m1,n1,rem,temp,gcd,lcm; printf("\n Enter two integer numbers "); scanf("%d%d",&m,&n); m=abs(m); n=abs(n); m1=m;n1=n; if(m==0) gcd=n; else if(n==0) gcd=m; else { rem=m%n; while(rem!=0) { m=n; n=rem; rem=m%n; } gcd=n; } lcm=m1*n1/gcd; printf("\n The GCD of %d and %d =%d",m1,n1,gcd); printf("\n The LCM of %d and %d =%d",m1,n1,lcm); getch(); } Q4 . Write a program to find the sum of N natural nos and print the sum using go to Statement. (6marks) Ans:- # include <stdio.h> main() { int a,N,i=1,sum=0; printf( \n enter the number & natural nos); scanf(%d,&N); label: printf(\n enter the numbers); (a) scanf(%d,&a); sum + = a;

i++; If(i<N) goto label; Printf(\n the sum=%d,sum); }

Q5 . write a C program to generate n Fibonacci series (5marks) Ans:#include<stdio.h> main() { int no,i; long int f1=0,f2=1,f3; printf("\n Enter the no.of FIBONACCI numbers required(<=47) "); scanf("%d",&no); printf("\n The FIBONACCI numbers are"); if(no==1) printf("\n %ld",f1); else if(no==2) printf("\n %ld\t%ld",f1,f2); else { printf("\n %ld\t%ld",f1,f2); i=2; do { f3=f1+f2; printf("\t %ld",f3); i++; f1=f2;f2=f3; } while(i<no); } getch(); } x2 x4 + - 2! 4!

Q6. Write a C-program to compute cos(x)=1-

Upto 15 terms and tabulate the values 0 to 1800 in steps of Ans: #include<stdio.h> #include<math.h> main()

100

(8marks)

{ int I,theta=0,den; float x,sum; while(theta<=1800) { x=theta * 3.1415/180; sum=1; term=1; den=2; for (I=1;I<15;I++) { term=-term*x*x/den; sum+=term; den+=2; } printf("\nthe calculated value of cos(%.d=%.4f",theta,sum); theta=theata+10; } } Q7. Ans:C provides three looping constructs for performing loop operations iv) The while statement v) The do statement vi) The for statement i) the while is a entry controlled loop statement, it tests the loop condition at the top of the loop, it the condition is true, then the body of the loop is executed, after execution of the body , it tests the condition again and it is true the loop executes once again, only when the condition is false the control is transferred out of the loop, and the program continues with the next statements while (test condition) { body of the loop ; ii) } the do statement called the co while loop checks its condition at the bottom of the loop, it is the do while loop always executes at least once, on reaching the do the program proceeds to evaluate the body of the loop , at the end of the loop the condition is checked it is true, the body of the loop is executed again, it is repeated till the condition is false, once it is false the control goes out of the while loop How do you perform looping in C ? Give the syntax of loop constructs in C? Compare the same ? (10marks)

do { body of the loop; } while ( test condition ); iii) the for loop is another entry controlled loop , the general form of for loop is for ( initialization; increment/decrement) { body of the loop; } The initialization of the control variables is done first, The value of the control variable is tested using th test condition, if the condition is true the body of the loop is executed, otherwise the loop is terminated When the body of the loop is executed, the control is transferred back to the for statement after the last statement in the loop, now the control variable is incremented or decremented, such as I++ or I -- new value is again tested and the process repeats test condition;

Q8.

Give an integer number, write a C program using while loop to reverse the digits of the number. (8marks) Ans:- /* Programme to reverse a given no */ # include <stdio.h> main() { int no, no1, rno=0,rem; printf ( \n enter an integer number ); scanf ( %d , &no); no1=no; while(no)

{ rem = no % 10; rno = rno*10 + rem; no/=10; } printf(the entered no is %d and the reversed number is %d,no1,no); } Q9. Explain the syntax of switch statement with an example. (6marks) Ans:Switch statement. Multiple branch selection statement Tests the value of an expression against a list of integer or char constants When a match is found, then statement associated with that constant is executed. switch(exp) { case const1 : st seq ; break ; case const2 : st seq ; break ; : : default : st seq ; }

Q10.

Explain while loop structure with an example. Write the flow chart also.(6)

Ans:- the while is a entry controlled loop statement, it tests the loop condition at the top of the loop, it the condition is true, then the body of the loop is executed, after execution of the body , it tests the condition again and it is true the loop executes once again, only when the condition is false the control is transferred out of the loop, and the program continues with the next statements while (test condition) { body of the loop ; }

Q11. Explain printf() with format specifiers. (6 marks) Ans:- The printf function is defined as follows: int printf(char *format, arg list ...) -prints to stdout the list of arguments according specified format string. Returns number of characters printed. Printf printf's name comes from print formatted. It generates

output under the control of a format string (its first argument) which consists of literal characters to be printed and also special character sequences--format specifiers--which request that other arguments be fetched, formatted, and inserted into the string. Our very first program was nothing more than a call to printf, printing a constant string:
printf("Hello, world!\n"); Our second program also featured a call to printf: printf("i is %d\n", i); In that case, whenever printf ``printed'' the string "i is %d", it did not print it verbatim; it replaced the two characters %d with the value of the variable i. There are quite a number of format specifiers for printf. Here are the basic ones : %d %ld %c %s %f %e %g %o %x
%%

print an int argument in decimal print a long int argument in decimal print a character print a string print a float or double argument same as %f, but use exponential notation use %e or %f, whichever is better print an int argument in octal (base 8) print an int argument in hexadecimal (base 16) print a single %

It is also possible to specify the width and precision of numbers and strings as they are inserted (somewhat like FORTRAN format statements); we'll present those details in a later chapter. (Very briefly, for those who are curious: a notation like %3d means to

print an int in a field at least 3 spaces wide; a notation like %5.2f means to print a float or double in a field at least 5 spaces wide, with two places to the right of the decimal.) Ex: printf("%c %d %f %e %s %d%%\n", '1', 2, 3.14, 56000000., "eight", 9); Output:1 2 3.140000 5.600000e+07 eight 9% The call printf("%d %o %x\n", 100, 100, 100); output:100 144 64 Successive calls to printf just build up the output a piece at a time, so the calls printf("Hello, "); printf("world!\n"); would also print Hello, world! (on one line of output). printf is one place where we get to make that choice: %d prints an integer value as a string of digits representing its decimal value, while %c prints the character corresponding to a character set value. So the lines char c = 'A'; int i = 97; printf("c = %c, i = %d\n", c, i); would print c as the character A and i as the number 97. But if, on the other hand, we called printf("c = %d, i = %c\n", c, i); Q12. List different control statement in C. Explain anyone with example. (8marks) Ans:- The different control statements in C are a) if statement b)switch statement c) conditional operator d) goto statements. Switch statement: Multiple branch selection statement Tests the value of an expression against a list of integer or char constants When a match is found, then statement associated with that constant is executed. switch(exp) { case const1 : st seq ; break ; case const2 : st seq ; break ; : : default : st seq ; }

Q13. With an example explain getch() and putchar() functions. (6marks) Ans:- getch() function :- this function is used to read a single charctor from the keyboard.the general syntax for this function is Character variale=getch(); Getch() does not wait for enter key to be pressed to assigned to read character to a variable and the entered character is not displayed on the monitor. putchar putchar puts its character argument on the standard output (usually the screen). The following example program converts any typed input into capital letters. To do this it applies the function toupper from the character conversion library ctype.h to each character in turn. #include <ctype.h> /* For definition of toupper */ #include <stdio.h> /* For definition of getchar, putchar, EOF */ main( ) { int ch; while((ch = getchar()) != EOF) putchar(toupper(ch)) } Ex:Main() { char ch; printf(\n enter any character); ch=getch(); printf(\nthe entered character is) putchar(ch); }

**********

Вам также может понравиться