C Programming Exercises on Control Flow and Loops
Branching Statements Practice
-
Wich statement about
switchis incorrect? (C) A. Thedefaultclause in aswitchstatement can be placed anywhere. B. The expression aftercasein aswitchstatement must be an integer constant expression. C.caseclauses must appear before thedefaultclause. D.caseexpressions do not need to be in order. -
What does this function return when called with
a = 1?int computeValue(int a) { int result; switch (a) { case 1: result = 30; case 2: result = 20; case 3: result = 16; default: result = 0; } return result; }Answer: 0 (due to fall-through without
break). -
In a
switch(c)statement,ccannot be of type: (D) A.intB.longC.charD.float -
Output of this code:
#include <stdio.h> int main() { int x = 3; int y = 3; switch (x % 2) { case 1: switch (y) { case 0: printf("first"); case 1: printf("second"); break; default: printf("hello"); } case 2: printf("third"); } return 0; }Output:
hellothird -
Output of this code:
#include <stdio.h> int main() { int i = 0; for (i = 0; i < 10; i++) { if (i = 5) printf("%d ", i); } return 0; }Output: Infinite loop printing
5(due to assignmenti = 5instead of comparison). -
Corrrect statement about
if: (C) A. Anifstatement can only be followed by one statement. B. Inif, 0 represents false and 1 represents true. C.ifis a branching statement that can implement single or multiple branches. D.elsealways matches its alignedifstatement. -
Compute the greatest common divisor (GCD) of two numbers. Example input:
20 40, output:20.#include <stdio.h> int main() { int num1, num2; scanf("%d %d", &num1, &num2); while (num2 != 0) { int remainder = num1 % num2; num1 = num2; num2 = remainder; } printf("%d", num1); return 0; } -
Print all leap years between 1000 and 2000.
#include <stdio.h> int main() { int year; for (year = 1000; year <= 2000; year++) { if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { printf("%d ", year); } } return 0; } -
Print all prime numbers between 100 and 200. Optimized method using trial division up to sqrt(i):
#include <stdio.h> #include <math.h> int main() { int i, j, count = 0; for (i = 101; i <= 200; i += 2) { int isPrime = 1; for (j = 2; j <= sqrt(i); j++) { if (i % j == 0) { isPrime = 0; break; } } if (isPrime) { count++; printf("%d ", i); } } printf("\nTotal primes: %d\n", count); return 0; } -
Sort three integers in descending order. Example input:
2 3 1, output:3 2 1.#include <stdio.h> int main() { int a, b, c; scanf("%d %d %d", &a, &b, &c); if (a < b) { int temp = a; a = b; b = temp; } if (a < c) { int temp = a; a = c; c = temp; } if (b < c) { int temp = b; b = c; c = temp; } printf("%d %d %d\n", a, b, c); return 0; } -
Print multiples of 3 from 1 to 100.
#include <stdio.h> int main() { int i; for (i = 1; i <= 100; i++) { if (i % 3 == 0) printf("%d ", i); } return 0; }
Loop Statements Practice
-
Regarding
while(condition)loop body, which is correct? (B) A. The loop body executes one more time than the condition. B. The condition executes one more time than the loop body. C. The condition and loop body execute the same number of times. D. The number of condition executions is unrelated to the loop body. -
Output of this code:
#include <stdio.h> int main() { int x = 0, y = 0; for (x = 1, y = 1; x <= 100; x++) { if (y >= 20) break; if (y % 3 == 1) { y += 3; continue; } y -= 5; } printf("%d\n", x); return 0; }Output:
8 -
Binary search in a sorted array.
#include <stdio.h> int main() { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int target = 3; int left = 0; int right = sizeof(arr) / sizeof(arr[0]) - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] > target) right = mid - 1; else if (arr[mid] < target) left = mid + 1; else { printf("Found at index: %d\n", mid); break; } } if (left > right) printf("Not found\n"); return 0; } -
Number guessing game.
#include <stdio.h> #include <stdlib.h> #include <time.h> void displayMenu() { printf("**************************\n"); printf("***** 1. Play Game *****\n"); printf("***** 0. Exit *****\n"); printf("**************************\n"); } void playGame() { int secret = rand() % 100 + 1; int guess; while (1) { printf("Guess a number: "); scanf("%d", &guess); if (guess == secret) { printf("Correct!\n"); break; } else if (guess > secret) printf("Too high\n"); else printf("Too low\n"); } } int main() { srand((unsigned)time(NULL)); int choice; do { displayMenu(); printf("Enter choice: "); scanf("%d", &choice); switch (choice) { case 1: playGame(); break; case 0: printf("Exiting\n"); break; default: printf("Invalid choice\n"); } } while (choice != 0); return 0; } -
Count occurrences of digit 9 in numbers 1 to 100.
#include <stdio.h> int main() { int i, count = 0; for (i = 1; i <= 100; i++) { if (i % 10 == 9) count++; if (i / 10 == 9) count++; } printf("%d\n", count); return 0; } -
Compute the sum: 1/1 - 1/2 + 1/3 - 1/4 + ... + 1/99 - 1/100.
#include <stdio.h> int main() { double total = 0.0; int sign = 1; for (int i = 1; i <= 100; i++) { total += sign * 1.0 / i; sign = -sign; } printf("%f\n", total); return 0; } -
Find the maximum of 10 integers.
#include <stdio.h> int main() { int numbers[10]; for (int i = 0; i < 10; i++) scanf("%d", &numbers[i]); int maxVal = numbers[0]; for (int i = 1; i < 10; i++) { if (numbers[i] > maxVal) maxVal = numbers[i]; } printf("Maximum: %d\n", maxVal); return 0; } -
Print a 9x9 multiplication table.
#include <stdio.h> int main() { for (int row = 1; row <= 9; row++) { for (int col = 1; col <= row; col++) { printf("%d*%d=%2d ", row, col, row * col); } printf("\n"); } return 0; }