Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

C Programming Exercises on Control Flow and Loops

Tech 3

Branching Statements Practice

  1. Wich statement about switch is incorrect? (C) A. The default clause in a switch statement can be placed anywhere. B. The expression after case in a switch statement must be an integer constant expression. C. case clauses must appear before the default clause. D. case expressions do not need to be in order.

  2. 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).

  3. In a switch(c) statement, c cannot be of type: (D) A. int B. long C. char D. float

  4. 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

  5. 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 assignment i = 5 instead of comparison).

  6. Corrrect statement about if: (C) A. An if statement can only be followed by one statement. B. In if, 0 represents false and 1 represents true. C. if is a branching statement that can implement single or multiple branches. D. else always matches its aligned if statement.

  7. 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;
    }
    
  8. 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;
    }
    
  9. 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;
    }
    
  10. 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;
    }
    
  11. 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

  1. 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.

  2. 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

  3. 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;
    }
    
  4. 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;
    }
    
  5. 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;
    }
    
  6. 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;
    }
    
  7. 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;
    }
    
  8. 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;
    }
    

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.