Practice Questions — Loops (for, while, do-while) and Patterns
← Back to NotesTopic-Specific Questions
Question 1
Easy
What is the output?
for (int i = 1; i <= 5; i++) {
cout << i << " ";
}i goes from 1 to 5 inclusive.
1 2 3 4 5 Question 2
Easy
What is the output?
for (int i = 5; i >= 1; i--) {
cout << i << " ";
}Counting downward from 5 to 1.
5 4 3 2 1 Question 3
Easy
What is the output?
int x = 1;
while (x <= 3) {
cout << "Hello" << endl;
x++;
}x starts at 1 and increments to 4.
HelloHelloHelloQuestion 4
Easy
What is the output?
int n = 10;
do {
cout << n << " ";
n++;
} while (n < 5);do-while executes the body at least once.
10 Question 5
Easy
What is the output?
for (int i = 0; i < 5; i++) {
cout << i << " ";
}Starts at 0, goes up to but not including 5.
0 1 2 3 4 Question 6
Medium
What is the output?
for (int i = 1; i <= 4; i++) {
if (i == 3) continue;
cout << i << " ";
}continue skips the rest of the current iteration.
1 2 4 Question 7
Medium
What is the output?
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
cout << i << j << " ";
}
}Inner loop runs completely for each outer iteration.
11 12 21 22 31 32 Question 8
Medium
How many times does "Hello" print?
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
cout << "Hello" << endl;
}
}Outer: 3 iterations. Inner: 4 iterations each.
12 timesQuestion 9
Medium
What is the output?
int sum = 0;
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) sum += i;
}
cout << sum;Sum of even numbers from 1 to 10.
30Question 10
Medium
What is the output?
int i = 5;
while (i > 0) {
cout << i << " ";
i -= 2;
}i decreases by 2 each time: 5, 3, 1, -1.
5 3 1 Question 11
Medium
What is the output?
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}Row i has i stars.
* * * * * * Question 12
Hard
What is the output?
for (int i = 0; i < 5; i++) {
if (i == 3) break;
cout << i << " ";
}break exits the loop entirely when i equals 3.
0 1 2 Question 13
Hard
What is the output?
int x = 1;
for (int i = 1; i <= 4; i++) {
x *= i;
}
cout << x;This computes 1 * 1 * 2 * 3 * 4.
24Question 14
Hard
What is the output?
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (i == j) continue;
cout << i << j << " ";
}
}continue in the inner loop skips pairs where i equals j.
12 13 21 23 31 32 Question 15
Hard
What is the output?
for (int i = 100; i >= 1; i /= 3) {
cout << i << " ";
}Integer division: 100/3=33, 33/3=11, 11/3=3, 3/3=1, 1/3=0.
100 33 11 3 1 Question 16
Hard
What is the output?
int num = 1;
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
cout << num++ << " ";
}
cout << endl;
}This is Floyd's Triangle. num increments continuously.
1 2 3 4 5 6 7 8 9 10 Question 17
Easy
What is the difference between a while loop and a do-while loop?
Think about when the condition is checked.
A
while loop checks the condition before each iteration; if false initially, the body never executes. A do-while loop checks the condition after each iteration, so the body always executes at least once.Question 18
Medium
In a range-based for loop, what is the difference between
for (auto x : arr) and for (auto& x : arr)?Think about copies vs references.
auto x creates a copy of each element. Modifying x does not change the array. auto& x creates a reference to each element. Modifying x directly changes the array element.Question 19
Hard
Explain the row-column approach to analyzing pattern problems. How do you determine the formula for spaces and stars?
Think about what changes from row to row.
The row-column approach: (1) Identify the number of rows (outer loop, variable i). (2) For each row, count the number of spaces and the number of characters (stars/numbers). (3) Express these counts as functions of i and the total rows n. For a pyramid: spaces = n - i, stars = 2*i - 1. (4) Write inner loops based on these formulas.
Question 20
Hard
What is the output?
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (j == 1) break;
cout << i << j << " ";
}
}break exits only the inner loop.
00 10 20 Mixed & Application Questions
Question 1
Easy
Write a program that prints even numbers from 2 to 20 using a for loop.
Start at 2, increment by 2.
for (int i = 2; i <= 20; i += 2) {
cout << i << " ";
}Question 2
Easy
What is the output?
int sum = 0;
for (int i = 1; i <= 5; i++) {
sum = sum + i;
}
cout << sum;Add 1 + 2 + 3 + 4 + 5.
15Question 3
Medium
What is the output?
int i = 0;
while (i < 5) {
i++;
if (i == 3) continue;
cout << i << " ";
}i is incremented before the continue check.
1 2 4 5 Question 4
Medium
Write a program to check if a number N is prime using a for loop.
Check divisors from 2 to sqrt(N). If any divides N, it is not prime.
#include
#include
using namespace std;
int main() {
int n;
cin >> n;
if (n <= 1) { cout << "Not prime" << endl; return 0; }
bool isPrime = true;
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) { isPrime = false; break; }
}
cout << (isPrime ? "Prime" : "Not prime") << endl;
return 0;
} Question 5
Medium
What is the output?
for (int i = 2; i <= 10; i += 3) {
cout << i << " ";
}i starts at 2 and increases by 3: 2, 5, 8, 11.
2 5 8 Question 6
Medium
What is the output?
int i = 1;
do {
cout << i << " ";
i *= 2;
} while (i <= 16);i doubles: 1, 2, 4, 8, 16, then 32.
1 2 4 8 16 Question 7
Hard
What is the output?
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 4 - i; j++) {
cout << " ";
}
for (int j = 1; j <= i; j++) {
cout << i << " ";
}
cout << endl;
}Row i has (4-i) double-spaces followed by i copies of the number i.
1 2 2 3 3 3 4 4 4 4 Question 8
Hard
What is the output?
int count = 0;
for (int i = 2; i <= 20; i++) {
bool isPrime = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) { isPrime = false; break; }
}
if (isPrime) count++;
}
cout << count;Count prime numbers from 2 to 20.
8Question 9
Hard
What is the output?
int arr[] = {3, 7, 2, 8, 1};
int n = 5;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int i = 0; i < n; i++) cout << arr[i] << " ";This is the bubble sort algorithm.
1 2 3 7 8 Question 10
Easy
Write a program that prints all numbers from 1 to 50 that are divisible by both 3 and 5.
n % 15 == 0 means divisible by both 3 and 5.
for (int i = 1; i <= 50; i++) {
if (i % 3 == 0 && i % 5 == 0) {
cout << i << " ";
}
}Question 11
Medium
What is the output?
int n = 5;
int result = 1;
for (int i = n; i >= 1; i--) {
result *= i;
}
cout << result;This computes 5 * 4 * 3 * 2 * 1.
120Question 12
Hard
What is the output?
int count = 0;
for (int i = 1; i <= 100; i++) {
if (i % 7 == 0) count++;
}
cout << count;Count multiples of 7 from 1 to 100.
14Question 13
Hard
What is the output?
for (int i = 1; i <= 4; i++) {
for (int j = i; j <= 4; j++) {
cout << j << " ";
}
cout << endl;
}Row i prints numbers from i to 4.
1 2 3 4 2 3 4 3 4 4 Question 14
Hard
What is the output?
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
cout << (i + j - 1) << " ";
}
cout << endl;
}Compute (i + j - 1) for each cell.
1 2 3 3 4 5 4 5 6 7 Multiple Choice Questions
MCQ 1
Which loop is best when the number of iterations is known in advance?
Answer: C
C is correct. The for loop is designed for counted iterations with its init-condition-update structure.
C is correct. The for loop is designed for counted iterations with its init-condition-update structure.
MCQ 2
How many times does the loop body execute?
for (int i = 0; i < 10; i++) { ... }Answer: B
B is correct. i takes values 0 through 9 (10 values). When i = 10, 10 < 10 is false.
B is correct. i takes values 0 through 9 (10 values). When i = 10, 10 < 10 is false.
MCQ 3
Which loop always executes its body at least once?
Answer: C
C is correct. do-while checks its condition after the body executes, guaranteeing at least one execution.
C is correct. do-while checks its condition after the body executes, guaranteeing at least one execution.
MCQ 4
What is the correct syntax for a range-based for loop in C++?
Answer: B
B is correct. C++11 range-based for syntax is
B is correct. C++11 range-based for syntax is
for (type variable : container).MCQ 5
What does the continue statement do inside a loop?
Answer: B
B is correct. continue skips the remaining body of the current iteration and jumps to the next iteration.
B is correct. continue skips the remaining body of the current iteration and jumps to the next iteration.
MCQ 6
Which creates an infinite loop in C++?
Answer: C
C is correct. for (;;) has no condition, which defaults to true, creating an infinite loop.
C is correct. for (;;) has no condition, which defaults to true, creating an infinite loop.
MCQ 7
What happens if you put a semicolon after for (int i = 0; i < 5; i++); ?
Answer: B
B is correct. The semicolon acts as an empty statement, becoming the loop body. The loop runs 5 times doing nothing.
B is correct. The semicolon acts as an empty statement, becoming the loop body. The loop runs 5 times doing nothing.
MCQ 8
In a range-based for loop, how do you modify array elements in-place?
Answer: B
B is correct. auto& creates a reference, allowing modifications to affect the original array. Plain auto creates a copy.
B is correct. auto& creates a reference, allowing modifications to affect the original array. Plain auto creates a copy.
MCQ 9
What does break do inside a nested loop?
Answer: B
B is correct. break exits only the innermost enclosing loop. The outer loop continues normally.
B is correct. break exits only the innermost enclosing loop. The outer loop continues normally.
MCQ 10
What is the time complexity of this code?
for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { ... } }Answer: C
C is correct. The outer loop runs n times, and for each, the inner loop runs n times. Total: n * n = O(n^2).
C is correct. The outer loop runs n times, and for each, the inner loop runs n times. Total: n * n = O(n^2).
MCQ 11
How many iterations does this loop execute?
for (int i = 1; i < 1000; i *= 2) { ... }Answer: B
B is correct. i doubles: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512. Next: 1024 >= 1000, stop. 10 iterations (log2(1000) is approximately 10).
B is correct. i doubles: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512. Next: 1024 >= 1000, stop. 10 iterations (log2(1000) is approximately 10).
MCQ 12
What is the output?
int x = 0; for (int i = 0; i < 3; i++) for (int j = 0; j <= i; j++) x++; cout << x;Answer: B
B is correct. i=0: j runs 0 to 0 (1 increment). i=1: j runs 0 to 1 (2 increments). i=2: j runs 0 to 2 (3 increments). Total: 1+2+3=6.
B is correct. i=0: j runs 0 to 0 (1 increment). i=1: j runs 0 to 1 (2 increments). i=2: j runs 0 to 2 (3 increments). Total: 1+2+3=6.
MCQ 13
Why is calling strlen(s) in a for loop condition a performance problem?
Answer: B
B is correct. strlen() scans the entire C-string to find \0, which is O(n). Calling it in every loop iteration makes the loop O(n^2) instead of O(n). Store the length in a variable before the loop.
B is correct. strlen() scans the entire C-string to find \0, which is O(n). Calling it in every loop iteration makes the loop O(n^2) instead of O(n). Store the length in a variable before the loop.
MCQ 14
What is the output?
for (int i = 0; i < 3; i++) cout << "Hi "; cout << "Done";Answer: A
A is correct. Without braces, only the first statement after for is in the loop body. So "Hi " prints 3 times, then "Done" prints once outside the loop.
A is correct. Without braces, only the first statement after for is in the loop body. So "Hi " prints 3 times, then "Done" prints once outside the loop.
Coding Challenges
Challenge 1: Sum of Digits
EasyTake an integer N from the user and calculate the sum of its digits using a while loop. Example: 1234 -> 1+2+3+4 = 10.
Sample Input
Enter a number: 1234
Sample Output
Sum of digits: 10
Use % 10 to get the last digit and / 10 to remove it.
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
int sum = 0, temp = n;
while (temp > 0) {
sum += temp % 10;
temp /= 10;
}
cout << "Sum of digits: " << sum << endl;
return 0;
}Challenge 2: Reverse a Number
EasyTake an integer and print its reverse. Example: 12345 -> 54321.
Sample Input
Enter a number: 12345
Sample Output
Reversed: 54321
Use a while loop with % 10 and / 10.
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
int reversed = 0;
while (n > 0) {
reversed = reversed * 10 + n % 10;
n /= 10;
}
cout << "Reversed: " << reversed << endl;
return 0;
}Challenge 3: Right-Aligned Number Triangle
MediumPrint a right-aligned triangle of numbers for N rows. Row i should have numbers 1 to i, right-aligned with leading spaces.
Sample Input
n = 5
Sample Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Use nested loops. Compute spaces as 2*(n-i).
#include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) cout << " ";
for (int j = 1; j <= i; j++) cout << j << " ";
cout << endl;
}
return 0;
}Challenge 4: Diamond Star Pattern
MediumPrint a diamond pattern of stars for N rows (upper half). The total height should be 2*N - 1.
Sample Input
n = 4
Sample Output
*
***
*****
*******
*****
***
*
Upper half: spaces = n-i, stars = 2*i-1. Lower half: mirror of the upper half.
#include <iostream>
using namespace std;
int main() {
int n = 4;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) cout << " ";
for (int j = 1; j <= 2 * i - 1; j++) cout << "*";
cout << endl;
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) cout << " ";
for (int j = 1; j <= 2 * i - 1; j++) cout << "*";
cout << endl;
}
return 0;
}Challenge 5: Pascal's Triangle
HardPrint the first N rows of Pascal's Triangle. Each element is the sum of the two elements above it.
Sample Input
n = 6
Sample Output
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Use the combinatorial formula: C(n, r) = C(n, r-1) * (n - r + 1) / r. No factorial functions needed.
#include <iostream>
using namespace std;
int main() {
int n = 6;
for (int i = 0; i < n; i++) {
int val = 1;
for (int j = 0; j <= i; j++) {
cout << val << " ";
val = val * (i - j) / (j + 1);
}
cout << endl;
}
return 0;
}Challenge 6: Hollow Rectangle
MediumPrint a hollow rectangle of stars with R rows and C columns. Only the border positions should have stars; interior positions should have spaces.
Sample Input
rows = 4, cols = 6
Sample Output
******
* *
* *
******
A position (i, j) is on the border if i == 1 or i == R or j == 1 or j == C.
#include <iostream>
using namespace std;
int main() {
int r = 4, c = 6;
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
if (i == 1 || i == r || j == 1 || j == c)
cout << "*";
else
cout << " ";
}
cout << endl;
}
return 0;
}Challenge 7: GCD Using Euclidean Algorithm
MediumFind the GCD of two numbers using the Euclidean algorithm with a while loop.
Sample Input
Enter two numbers: 48 18
Sample Output
GCD: 6
Use while loop: while (b != 0) { temp = b; b = a % b; a = temp; }
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
int origA = a, origB = b;
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
cout << "GCD of " << origA << " and " << origB << ": " << a << endl;
return 0;
}Challenge 8: Armstrong Number Checker
HardCheck if a number is an Armstrong number. An N-digit number is Armstrong if the sum of its digits, each raised to the power N, equals the number itself. Example: 153 = 1^3 + 5^3 + 3^3 = 153.
Sample Input
Enter a number: 153
Sample Output
153 is an Armstrong number
First count digits, then compute sum of powers using a loop.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
int digits = 0, temp = n;
while (temp > 0) { digits++; temp /= 10; }
int sum = 0;
temp = n;
while (temp > 0) {
int d = temp % 10;
sum += pow(d, digits);
temp /= 10;
}
cout << n << (sum == n ? " is" : " is not") << " an Armstrong number" << endl;
return 0;
}Need to Review the Concepts?
Go back to the detailed notes for this chapter.
Read Chapter NotesWant to learn C++ with a live mentor?
Explore our C++ Masterclass