Loops
1. Which of the following statements are true.
a. for loop control statement is executed at end of every iteration.
Ans: False, it is executed at beginning of every iteration.
b. for loop control statement is executed at beginning of every iteration.
Ans: True.
c. Given a for loop, for(i=0; 1; i++), control will skip loop body and jump to end of the loop.
Ans: False. Control statement is always true. Control will enter for loop.
d. for loop initialization statement is executed at the beginning of every iteration.
Ans: False. It is executed only once at beginning of first iteration.
e. for loop tail statement is executed at beginning of every iteration.
Ans: False. Tail statement is executed at end of every iteration.
2. Given a for loop, for(i=0; 0; i++), what will be the value of i at the end of the loop?
Ans: 0. Loop control expression is false, hence loop will never be executed and tail statement will never be executed.
3. Write a program that uses for loop to print numbers from 10 to 1, both inclusive.
#include <stdio.h> int main(int argc, char * argv[]) { int i=0; for(i=10; i>0; i--){ printf("%d\n", i); } return 0; }
4. Write a program to print multiplication table of 2.
#include <stdio.h> int main(int argc, char * argv[]) { int i=1; for(; i<=10; i++){ printf(" 2 X %d = %d\n", i, 2*i); } return 0; }
5. Write a program to print squares of first ten natural numbers
#include <stdio.h> int main(int argc, char * argv[]) { int i=1; for(; i<=10; i++){ printf("Square of %d is %d\n", i, i*i); } return 0; }
6. Which of the following statements are true.
a. In a for loop, initialization statement is optional.
Ans: True
b. If there are multiple comma separated statements in loop control expression then only rightmost statement is executed.
Ans: False. Rightmost statement is the one which will control but all statements will be executed.
c. If for loop has loop initialization statement then it must also have a tail statement.
Ans: False
7. Which of the following loop constructs are valid.
a.
float a=0, pi=3.14; for (a=0.2 ; a < 7.1; a=a*pi) { printf("%f ",a); }
Ans: Valid.
b.
for (a=0,b=1; a<b) { printf("%d %d \n",a,b); a++; b--; }
Ans: Invalid. for loop construct has only 2 statements – initialization and control expression.
8. What will be the output of following code snippets.
a.
for (a=2,b=3,c=7; a<b+c; a++) { if (c>b) { c--; } if (b>a) { a++; } } printf("%d %d %d\n",a,b,c);
Ans: 7 3 3
b.
int a=3, b=5, c=10; if(a+b<c) { for(;a<c;a++){ b++; } } else { for(;c<a;a--){ b--; } } printf("%d %d %d\n",a,b,c);
Ans: 10 12 10
c.
int a=3, b=5, c=10; for(;a>b;){ a--; b++; c--; } printf("%d %d %d\n",a,b,c);
Ans: 3 5 10
9. Write a program that reads a positive number (n) as user input and then prints sum of first n natural numbers. The user input should be in the range 1 to 1000. If input is outside this range, program should print an error message.
#include <stdio.h> int main(int argc, char * argv[]) { int num = 0, i = 0, sum = 0; printf("Enter a number [1-1000]: "); scanf("%d", &num); if (num < 1 || num > 1000) { printf("Invalid Input\n"); } else { for (i=1; i <= num; i++) { sum += i; } printf("Sum of 1 to %d is %d\n", num, sum); } return 0; }
10. Write a program that uses for loop to print first 5 fibonacci numbers.
#include <stdio.h> int main(int argc, char * argv[]) { int f0 = 1, f1 = 1, f = 0, i = 0; printf("Fibonacci series: %d %d", f0, f1); for(i=0; i<3; i++) { f = f0 + f1; printf(" %d", f); f0 = f1; f1 = f; } return 0; }
11. What will be the output of following program.
#include <stdio.h> int main (int argc, char * argv[]) { int a=0, b=1, sum=0; for (; a*b<=100; a=a+2, b++) { sum = sum + (a*b); } printf("%d\n", sum); return 0; }
Ans: 224
12. Which of the following statements are true.
a. while loop is more powerful than for loop.
Ans: False. Whatever can be written using one construct can be written using the other.
b. It is not possible to count down using a while loop.
Ans: False.
c. Control expression in while loop is optional.
Ans: False. while must have a control expression.
d. while(0) is a valid loop construct.
Ans: True.
e. If control condition of while loop is false then control will jump to end of loop.
Ans: True.
13. What will be the output of following code snippets.
a.
int i=1; while (i<5) { printf("%d\n",i+1); i++; }
Ans:
2
3
4
5
b.
int a=1, b=3, c=7; while (a<b && c-b>a) { if (a+b>2) { printf("%d %d %d",a,b,c); b--; } a++; b--; c++; }
Ans: 1 3 7
14. Write a program that reads a positive number (n) as user input and then prints sum of first n natural numbers. The user input should be in the range 1 to 1000. If input is outside this range, program should print an error message. Use while loop construct in the program.
#include <stdio.h> int main(int argc, char * argv[]) { int num = 0, i = 1, sum = 0; printf("Enter a number [1-1000]: "); scanf("%d", &num); if (num < 1 || num > 1000) { printf("Invalid Input\n"); } else { while(i <= num) { sum += i; i++; } printf("Sum of 1 to %d is %d\n", num, sum); } return 0; }
15. Write a program using while loop that determines the number of digits in an integer. E.g. if input was 25689 then output should be 5.
#include <stdio.h> int main(int argc, char * argv[]) { int num = 0, digits = 0; printf("Enter a number: "); scanf("%d", &num); printf("%d has ", num); if (0 == num) { digits = 1; } else { while(num) { num /= 10; digits++; } } printf("%d digits\n", digits); return 0; }
16. What is the output of following code snippet?
int a=7, b=11, c=17; while (a<c) { if (a+c > b+c) { c++; break; } a+=2; b--; } printf("%d %d %d\n",a,b,c);
Ans: 11 9 18
17. Trace control flow of prime number program for user input 7.
01: #include <stdio.h> 02: int main (int argc, char * argv[]) { 03: int num = 0; 04: int i = 0; 05: printf("Please enter a number greater than 2: "); 06: scanf ("%d", &num); 07: if (num <= 2) { 08: printf ("Invalid input. Exiting\n"); 09: } else { 10: for (i=2; i<=num/2; i++) { 11: if (0 == num%i ) { 12: break; 13: } 14: } 15: if (i <= num/2) { 16: printf ("%d is not a prime number\n",num); 17: } else { 18: printf ("%d is a prime number\n",num); 19: } 20: } 21: return 0; 22: }
- After user input, control reaches line 07. Control expression evaluates to false. Control jumps to else, line 09.
- Control advances to line 10, for loop. i is initialized to 2. Control expression 2 <= 3 evaluates to true. Control enters for loop, line 11.
- Control expression 0 == 7%2 evaluates to false. Control jumps to end of if and end of loop.
- Loop tail statement is executed, value of i becomes 3.
- Loop control expression 3 <= 3 evaluates to true. Control enters for loop, line 11.
- Control expression 0 == 7%3 evaluates to false. Control jumps to end of if and end of loop.
- Loop tail statement is executed, value of i becomes 3.
- Loop control expression 3 <= 3 evaluates to false. Control jumps outside for loop, line 15.
- if control expression 4 <= 3 evaluates to false. Control jumps to else block, line 17.
- Line 18, message 7 is a prime number is printed.
- Control advances to end of else, and end of program and terminates.
18. Write a program that determines whether a given number is a perfect square or not.
#include <stdio.h> int main(int argc, char * argv[]) { int num = 0, i = 1; printf("Enter a number >1: "); scanf("%d", &num); if (num < 1) { printf("Invalid Input\n"); } else { while (i*i <= num) { if(i*i == num) { printf("%d is a perfect square\n", num); break; } i++; } if(num != i*i) { printf("%d is not a perfect square\n", num); } } return 0; }
19. Which of the following statements are true.
a. for, while, and do while loops are equal in capability. Code written using one loop construct can be rewritten using any other loop construct.
Ans: True.
b. while loop body is guaranteed to be executed at least once.
Ans: False. while checks loop condition at beginning. If condition is false for first iteration, control will never enter loop body.
c. do while loop body is guaranteed to be executed at least once.
Ans: True. do while checks loop condition at end of loop. Control always enters do while loop body at least once.
d. Control condition of do while loop is optional.
Ans: False.
20 What is the output of following code snippet.
int a=1, b=3, c=2; do { if (a+b>c) { c+=2; a--; } else if (!a) { break; } }while(a||b||c); printf("%d %d %d\n",a,b,c);
Ans: 0 3 4
21 Write a program to print multiplication table of 7 using a do while loop.
#include <stdio.h> int main(int argc, char * argv[]) { int i=1; do { printf("7 X %d = %d\n", i, 7*i); i++; }while(i<=10); return 0; }
22 Write an addition program that reads values from user and keeps on adding them until user enters 0. On receiving 0 input, program prints the computed sum and terminates.
#include <stdio.h> int main(int argc, char * argv[]) { int num = 0, sum = 0; do { printf("Enter a number to add [0 to quit]: "); scanf("%d", &num); sum += num; }while(num != 0); printf("Sum is %d\n", sum); return 0; }
23 Write a program to print all factors of a number.
#include <stdio.h> int main(int argc, char * argv[]) { int num = 0, i = 1; printf("Enter a number > 1: "); scanf("%d", &num); printf("Factors are "); do { if (num % i == 0) { printf("%d ", i); } i++; }while(i<=num/2); printf("%d\n", num); return 0; }
24 Write a program to find sum of all digits of an integer. E.g. if user inputs 1590, program should print 15 (1 + 5 + 9 + 0).
#include <stdio.h> int main(int argc, char * argv[]) { int num = 0, sum = 0; printf("Enter a number: "); scanf("%d", &num); do { sum += num%10; num /= 10; }while(num); printf("sum of digits = %d\n", sum); return 0; }
25 Trace the control flow of infinite loop example above
01: #include <stdio.h> 02: int main (int argc, char * argv[]) { 03: int i=1; 04: for(;;i++) { 05: if (i>10) { 06: break; 07: } 08: printf("2 X %d = %d\n", i, 2*i); 09: } 10: return 0; 11: }
- After initialization control reaches line 04. Infinite loop with empty initialization, control enters loop body, line 05.
- 1 > 10 evaluates to false. Control jumps to end of if, line 08.
- Message 2 X 1 = 2 is printed to the console.
- Control reaches end of loop. Tail statement is executed. Value of i becomes 2. Control reaches line 05.
- 2 > 10 evaluates to false. Control jumps to end of if, line 08.
- Message 2 X 2 = 4 is printed to the console.
- Control reaches end of loop. Tail statement is executed. Value of i becomes 3. Control reaches line 05.
- 3 > 10 evaluates to false. Control jumps to end of if, line 08.
- Message 2 X 3 = 6 is printed to the console.
- Control reaches end of loop. Tail statement is executed. Value of i becomes 4. Control reaches line 05.
- 4 > 10 evaluates to false. Control jumps to end of if, line 08.
- Message 2 X 4 = 8 is printed to the console.
- Control reaches end of loop. Tail statement is executed. Value of i becomes 5. Control reaches line 05.
- 5 > 10 evaluates to false. Control jumps to end of if, line 08.
- Message 2 X 5 = 10 is printed to the console.
- Control reaches end of loop. Tail statement is executed. Value of i becomes 6. Control reaches line 05.
- 6 > 10 evaluates to false. Control jumps to end of if, line 08.
- Message 2 X 6 = 12 is printed to the console.
- Control reaches end of loop. Tail statement is executed. Value of i becomes 7. Control reaches line 05.
- 7 > 10 evaluates to false. Control jumps to end of if, line 08.
- Message 2 X 7 = 14 is printed to the console.
- Control reaches end of loop. Tail statement is executed. Value of i becomes 8. Control reaches line 05.
- 8 > 10 evaluates to false. Control jumps to end of if, line 08.
- Message 2 X 8 = 16 is printed to the console.
- Control reaches end of loop. Tail statement is executed. Value of i becomes 9. Control reaches line 05.
- 9 > 10 evaluates to false. Control jumps to end of if, line 08.
- Message 2 X 9 = 18 is printed to the console.
- Control reaches end of loop. Tail statement is executed. Value of i becomes 10. Control reaches line 05.
- 10 > 10 evaluates to false. Control jumps to end of if, line 08.
- Message 2 X 10 = 20 is printed to the console.
- Control reaches end of loop. Tail statement is executed. Value of i becomes 11. Control reaches line 05.
- 11 > 10 evaluates to true. Control enters if block, line 06.
- break causes control to jump out of for loop. Line 10.
- Program terminates.
26. Using an infinite loop write an addition program that reads values from user and keeps on adding them until user enters 0. On receiving 0 input, program prints the computed sum and terminates.
#include <stdio.h> int main(int argc, char * argv[]) { int num = 0, sum = 0; do { printf("Enter a number to add [0 to quit]: "); scanf("%d", &num); if (!num) { break; } sum += num; }while(1); printf("Sum is %d\n", sum); return 0; }
27. Trace the control flow of addition calculator above for following sequences of user inputs.
-9, -10, 0
-3, -4, 4, 3, 7, -7, 0
0
1, 2, 3, 4, 5, 0
28 Which of the following statements are true.
continue is a C language keyword.
Ans: True.
continue in a for loop causes control to jump to beginning of loop and hence loop tail statement is skipped for that iteration.
Ans: False. Tail statement is executed.
In following code snippet, break statement will never be executed.
while(…) { … if(a>10){ continue; break; } }
Ans: True.
29 Write a program that reads in 10 integers from the user and prints the sum of all positive numbers in the input.
#include <stdio.h> int main(int argc, char * argv[]) { int num = 0, sum = 0, i = 0; do { printf("Enter a number to add: "); scanf("%d", &num); if (num>0) { sum += num; } i++; }while(i<10); printf("Sum is %d\n", sum); return 0; }
30 Which of the following statements are true.
a. for loop can be nested only in another for loop.
Ans: False. We can nest any loop construct within any other loop construct.
b. Loops can be nested only to maximum 5 levels.
Ans: False. There is no limit on nesting.
c. A break in nested loop will cause the control to jump to end of outer loop.
Ans: False. Control jumps out of current loop.
d. A continue in nested loop will cause control to jump to next iteration of nested loop.
Ans: True.
31 What will be the output of below program?
#include <stdio.h> int main (int argc, char * argv[]) { int i = 0, j =0, sum = 0; while (i<10) { for (j=3; j>0; j--) { sum = sum + i + j; } i++; } /* Errata, following print statement is missing in printed book */ printf("%d\n",sum); return 0; }
Ans: 195
32 Write a program that reads a number n, in the range of 5 to 100, from user and prints nth prime number. E.g. if user input was 5, program should print 5th prime number 11.
#include <stdio.h> int main (int argc, char * argv[]) { int nth = 0, num = 2, count = 0, i = 0; printf("Enter a number (5-100) "); scanf("%d", &nth); if(nth < 5 || nth > 100) { printf("Invalid input\n"); } else { do { for(i=2; i<=num/2; i++) { if(num%i == 0) { break; } } if(i>num/2) { printf("Prime: %d\n", num); count++; } if (count == nth) { printf("%dth prime nunmber is %d\n",nth, num); break; } num++; } while(1); } return 0; }
33 Write a program to print all alphabets from ‘A’ to ‘Z’.
#include <stdio.h> int main (int argc, char * argv[]) { char c = 'A'; for(;c<='Z';c++){ printf("%c ", c); } return 0; }
34 Modify the above program to print ‘A’ to ‘Z’ with alternate characters in lower case, i.e. A b C d E and so on.
#include <stdio.h> int main (int argc, char * argv[]) { char c = 'A', count = 1; for(;c<='Z';c++, count++){ if(count%2 != 0) { printf("%c ", c); } else { printf("%c ", c-'A'+'a'); } } return 0; }