Skip to content
Laviyx Learn

Try

C++ Control Flow Practice

Practise if/else, switch, for and while loops, nested loops, break and continue in C++.

Beginner7 problemsUpdated

Review first: if, else if and else, switch, Loops: for, while, do-while, for Loops in Depth, while and do-while, break and continue

  1. 1.Marks to grades

    Beginner

    For each mark in {95, 82, 67, 45, 74}, print the mark and its grade: A for 90 and above, B for 75–89, C for 60–74 and F below 60.

    Show hint

    Use an if / else if chain, checking from the highest grade down.

    Show solution
    C++
    #include <iostream>
    #include <vector>
    
    int main() {
        std::vector<int> marks = {95, 82, 67, 45, 74};
    
        for (int m : marks) {
            char grade;
            if (m >= 90) {
                grade = 'A';
            } else if (m >= 75) {
                grade = 'B';
            } else if (m >= 60) {
                grade = 'C';
            } else {
                grade = 'F';
            }
            std::cout << m << ' ' << grade << '\n';
        }
    }
    Output
    95 A
    82 B
    67 C
    45 F
    74 C
  2. 2.FizzBuzz

    Beginner

    Print the numbers 1 to 15 on one line, separated by spaces, but print Fizz for multiples of 3, Buzz for multiples of 5, and FizzBuzz for multiples of both.

    Show hint

    Check n % 15 == 0 first, otherwise 15 is caught by the multiple-of-3 branch.

    Show solution
    C++
    #include <iostream>
    
    int main() {
        for (int n = 1; n <= 15; ++n) {
            if (n % 15 == 0) {
                std::cout << "FizzBuzz";
            } else if (n % 3 == 0) {
                std::cout << "Fizz";
            } else if (n % 5 == 0) {
                std::cout << "Buzz";
            } else {
                std::cout << n;
            }
            std::cout << (n < 15 ? ' ' : '\n');
        }
    }
    Output
    1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz
  3. 3.Digit sum and reverse

    Beginner

    For n = 50721, use a while loop to print the sum of its digits and the number reversed.

    Show hint

    Take the last digit with n % 10, then remove it with n /= 10, until n is 0. Build the reverse with reversed = reversed * 10 + digit.

    Show solution
    C++
    #include <iostream>
    
    int main() {
        int n = 50721;
        int sum = 0;
        int reversed = 0;
    
        while (n > 0) {
            int digit = n % 10;
            sum += digit;
            reversed = reversed * 10 + digit;
            n /= 10;
        }
        std::cout << sum << ' ' << reversed << '\n';
    }
    Output
    15 12705
  4. 4.Weekday or weekend

    Beginner

    Days are numbered 1 (Monday) to 7 (Sunday). For each of 1, 6, 7 and 9, print weekday, weekend, or invalid using a switch.

    Show hint

    Group cases that share an action, and use default for anything outside 1–7.

    Show solution
    C++
    #include <iostream>
    
    int main() {
        for (int day : {1, 6, 7, 9}) {
            std::cout << day << ": ";
            switch (day) {
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                    std::cout << "weekday\n";
                    break;
                case 6:
                case 7:
                    std::cout << "weekend\n";
                    break;
                default:
                    std::cout << "invalid\n";
            }
        }
    }
    Output
    1: weekday
    6: weekend
    7: weekend
    9: invalid
  5. 5.First common multiple

    Beginner

    Find the first number greater than 100 that is divisible by both 7 and 11, using a loop with break.

    Show hint

    Loop upwards from 101 with no end condition, and break as soon as both divisions leave no remainder.

    Show solution
    C++
    #include <iostream>
    
    int main() {
        int n = 101;
        while (true) {
            if (n % 7 == 0 && n % 11 == 0) {
                break;
            }
            ++n;
        }
        std::cout << n << '\n';
    }
    Output
    154

    A number divisible by both 7 and 11 is a multiple of 77, so the answer is 2 × 77 = 154.

  6. 6.Star pyramid

    Beginner

    Print a centred pyramid of stars with 4 rows: row r has 4 - r spaces followed by 2r - 1 stars.

    Show hint

    Use an outer loop for rows and two inner loops: one for spaces, one for stars.

    Show solution
    C++
    #include <iostream>
    
    int main() {
        int height = 4;
        for (int r = 1; r <= height; ++r) {
            for (int i = 0; i < height - r; ++i) std::cout << ' ';
            for (int i = 0; i < 2 * r - 1; ++i) std::cout << '*';
            std::cout << '\n';
        }
    }
    Output
       *
      ***
     *****
    *******
  7. 7.Skip the negatives

    Beginner

    Add up only the positive numbers in {5, -2, 8, -7, 3}, using continue to skip the others. Print the total.

    Show hint

    At the top of the loop body, if (x < 0) continue;.

    Show solution
    C++
    #include <iostream>
    
    int main() {
        int total = 0;
        for (int x : {5, -2, 8, -7, 3}) {
            if (x < 0) continue;
            total += x;
        }
        std::cout << total << '\n';
    }
    Output
    16