C++ Control Flow
Find the largest of three numbers
Description: Write largest(int a, int b, int c). Test with (12, 45, 30) and (7, 7, 3).
Hint:
- Compare with
>=and&&, or callstd::maxwith a list.
Solution
#include <algorithm>
#include <iostream>
int largest(int a, int b, int c) {
if (a >= b && a >= c) return a;
if (b >= c) return b;
return c;
}
int main() {
std::cout << largest(12, 45, 30) << '\n';
std::cout << largest(7, 7, 3) << '\n';
std::cout << std::max({12, 45, 30}) << '\n'; // the library way
}
45
7
45
How it works
- If
ais not the biggest, the answer isborc, so one more check is enough.
Read C++ if else.
Check if a year is a leap year
Description: Write is_leap(int year). Test 1900, 2000, 2024 and 2026.
Hint:
- Use
%and the three rules.
Solution
#include <iostream>
bool is_leap(int year) {
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
int main() {
std::cout << std::boolalpha;
for (int y : {1900, 2000, 2024, 2026}) {
std::cout << y << " -> " << is_leap(y) << '\n';
}
}
1900 -> false
2000 -> true
2024 -> true
2026 -> false
How it works
- 1900 is divisible by 100 but not by 400, so it is not a leap year. 2000 is divisible by 400, so it is.
Read C++ Operators.
FizzBuzz in C++
Description: Print 1 to 15. For multiples of 3 print Fizz, for multiples of 5 Buzz, for both FizzBuzz.
Hint:
- Check the multiples of 15 first.
Solution
#include <iostream>
int main() {
for (int i = 1; i <= 15; i++) {
if (i % 15 == 0) std::cout << "FizzBuzz\n";
else if (i % 3 == 0) std::cout << "Fizz\n";
else if (i % 5 == 0) std::cout << "Buzz\n";
else std::cout << i << '\n';
}
}
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
How it works
- The most special case (15) must be checked first, or it would print only
Fizz.
Read C++ for Loop.
Sum of the digits of a number
Description: Write digit_sum(int n). For 9475 the answer is 25.
Hint:
n % 10is the last digit,n / 10removes it.
Solution
#include <iostream>
int digit_sum(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
int main() {
std::cout << digit_sum(9475) << ' ' << digit_sum(100) << ' ' << digit_sum(0) << '\n';
}
25 1 0
How it works
- The loop adds the last digit and cuts it off, until the number is 0.
Read C++ while Loop.
Print a multiplication table
Description: Print 7 x 3 = 21 for 1 to 10.
Hint:
- A for loop and a format with a width, like
{:2}.
Solution
#include <print>
int main() {
int n = 7;
for (int i = 1; i <= 10; i++) {
std::println("{} x {:2} = {:2}", n, i, n * i);
}
}
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
How it works
{:2}uses at least 2 places, so the numbers line up.
Read C++ for Loop.
Check prime numbers and list the primes up to 30
Description: Write is_prime(int n) and print the primes from 1 to 30.
Hint:
- Test divisors while
i * i <= n.
Solution
#include <iostream>
bool is_prime(int n) {
if (n < 2) return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
int main() {
for (int n = 1; n <= 30; n++) {
if (is_prime(n)) std::cout << n << ' ';
}
std::cout << '\n';
}
2 3 5 7 11 13 17 19 23 29
How it works
- If
nhas a divisor bigger than its square root, it also has a smaller one, so we can stop early.
Read C++ Functions.