Loops & Iterations¶
Repetition is what computers do best. These problems will help you master for, while, and
do-while loops, as well as nested loops for patterns.
1. Print 1 to N¶
Rating: Newbie | Difficulty: Easy
Input a number N and print all numbers from 1 to N. 💡 Hint
Use a
for loop starting from 1 to N.
2. Even Numbers from 1 to N¶
Rating: Newbie | Difficulty: Easy
Input N and print all even numbers between 1 and N. 💡 Hint
Use if
(i % 2 == 0) inside the loop, or increment the loop by 2: for (int i = 2; i <= N; i += 2).
3. Factorial of a Number¶
Rating: Beginner | Difficulty: Easy
Input a number and calculate its factorial (n!). E.g., 5! = 5 * 4 * 3 * 2 * 1 = 120.
Initialize fact = 1 and multiply it by loop counter i in each
iteration.
4. Multiplication Table¶
Rating: Beginner | Difficulty: Easy
Input a number and print its multiplication table up to 10. 💡 Hint
print(num + " x " + i + " = " + (num * i)).
5. Sum of Digits¶
Rating: Intermediate | Difficulty: Medium
Input a number and calculate the sum of its digits. E.g., 123 -> 1 + 2 + 3 = 6.
Use num % 10 to get the last digit and num / 10 to remove it in a
while loop.
6. Reverse a Number¶
Rating: Intermediate | Difficulty: Medium
Input a number and print its reverse. E.g., 123 -> 321. 💡 Hint
reverse = (reverse * 10) + (num % 10).
7. Palindrome Number¶
Rating: Intermediate | Difficulty: Medium
Check if a number is a palindrome (reads the same forward and backward). E.g., 121 is a
palindrome. 💡 Hint
Reverse the number and compare it with the original
value.
8. Fibonacci Series¶
Rating: Beginner | Difficulty: Easy
Print the first N terms of the Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13... 💡
Hint
Start with a = 0, b = 1. In each step, next = a + b, then update a = b and b =
next.
9. Prime Number Checker¶
Rating: Intermediate | Difficulty: Medium
Input a number and check if it is prime (divisible only by 1 and itself). 💡
Hint
Check divisibility from 2 up to √num. If any number divides it, it's not prime.
10. Armstrong Number¶
Rating: Advanced | Difficulty: Medium
Check if a number is an Armstrong number. (Sum of cubes of digits equals the number itself, e.g.,
153 = 1³ + 5³ + 3³). 💡 Hint
Extract each digit, cube it, and add to a
sum.
11. GCD (Greatest Common Divisor)¶
Rating: Advanced | Difficulty: Medium
Find the GCD of two numbers. 💡 Hint
Use the Euclidean algorithm or a
loop from 1 to the smaller of the two numbers.
12. Power of a Number¶
Rating: Beginner | Difficulty: Easy
Input base and exponent. Calculate base^exponent without using built-in math functions.
Run a loop exponent times and multiply base each time.
13. Star Pattern (Right Triangle)¶
Rating: Intermediate | Difficulty: Medium
Print the following pattern for N rows:
💡 Hint
Use nested loops. Outer loop for rows, inner loop for columns (`j <= i`).14. Number Pattern¶
Rating: Intermediate | Difficulty: Medium Print the following pattern:
💡 Hint
Similar to star pattern, but print the inner loop counter `j`.15. Perfect Number¶
Rating: Pro | Difficulty: Medium
Check if a number is a Perfect Number (sum of its proper divisors equals the number, e.g., 6 = 1 +
2 + 3). 💡 Hint
Loop from 1 to num/2 and add the divisors.