C++ and Mathematics
Conditions and loops
if/else, logical operators, for and while loops, sums and products in code, nested loops.
50 min 8 questions
01Conditions
if (condition) { ... } else { ... }. Comparisons: ==, !=, <, <=, >, >=; logic: && (and), || (or), ! (not). A classic bug: = assigns, == compares.
Divisibility is checked with the remainder: n % d == 0 means .
02Loops and sums
A sum is a loop in code: start with s = 0 (p = 1 for a product!) and add one term per step. for (int i = 1; i <= n; i++) visits .
while (condition) repeats as long as the condition is true — useful when the number of iterations is not known in advance (e.g. "how many times must we multiply by 3 to exceed 100?").
int s = 0;
for (int i = 1; i <= n; i++) {
s += i; // s = 1 + 2 + ... + n = n(n+1)/2
}03Nested loops
A loop inside a loop: the inner loop runs completely for every step of the outer one. If the outer runs times and the inner times, there are iterations. If the inner loop starts at j = i, the count is .
Key formulas
Sum formulas (to check your loops)
Geometric sum
Worked examples
Example 1
Write a program counting the numbers from 1 to 100 divisible by 3 or 5.
int c = 0;
for (int i = 1; i <= 100; i++)
if (i % 3 == 0 || i % 5 == 0) c++;
cout << c; // 47What does this code print?
#include <iostream>
using namespace std;
int main() {
int s = 0;
for (int i = 1; i <= 10; i++) s += i;
cout << s;
}Sample content: questions and lessons are illustrative and are not taken from official exams.
