- Writing
while,do-while, andforloops for real problems - Designing correct loop termination conditions and avoiding infinite loops
- Implementing the Euclidean algorithm (GCD) with a
whileloop and repeated subtraction - Computing LCM, coprime checks, and common divisors with loops
- Counting, extracting, and analyzing the digits of an integer
- Converting between binary, octal, decimal, and hexadecimal using loops
- Counting set bits (popcount) with a
whileloop - Building nested loops for Pascal's triangle, Floyd's triangle, X, plus, and number pyramids
- Computing series such as ∑1/n², ∑n!, and Tribonacci numbers
- Finding the largest prime factor of an integer with a
whileloop - Implementing iterative binary search using a
whileloop - Checking whether two numbers are amicable using a
forloop
Loops are the engine of every non-trivial C program. This page collects 30 looping / iterative statement assignments that go beyond the usual "print 1 to N" warm-ups. You'll implement the Euclidean algorithm, dissect integers digit by digit, convert between number systems, draw advanced star and number patterns with nested loops, and finish with classic interview algorithms like iterative binary search, largest prime factor, and amicable numbers. Every assignment links to a full tutorial with the program code, sample output, and a line-by-line explanation.
🔹 GCD, LCM & Divisibility
Classic number-theory loops — Euclid's algorithm, multiples, common divisors, and coprime checks.
- 1. Write a program in C to find the GCD (Greatest Common Divisor) of two numbers using a while loop (Euclidean algorithm).
- 2. Write a program in C to find the LCM (Least Common Multiple) of two numbers using a for loop.
- 3. Write a program in C to check whether two numbers are coprime using a while loop.
- 4. Write a program in C to find all common divisors of two numbers using a for loop.
- 5. Write a program in C to calculate the GCD of three numbers using nested loops.
🔹 Digit Statistics & Operations
Extract and analyze the individual digits of an integer with while, do-while, and for loops.
- 6. Write a program in C to count the number of digits in an integer using a while loop.
- 7. Write a program in C to find the largest digit in an integer using a do-while loop.
- 8. Write a program in C to find the smallest digit in an integer using a while loop.
- 9. Write a program in C to count the occurrences of a specific digit (e.g., 5) in an integer using a for loop.
- 10. Write a program in C to calculate the product of all digits of an integer using a while loop.
- 11. Write a program in C to check whether an integer is a single-digit number using a do-while loop.
- 12. Write a program in C to output the digits of an integer in reverse order using a while loop (digit by digit, without reconstructing the number).
- 13. Write a program in C to determine whether all digits of an integer are even using a for loop.
🔹 Number-System Conversion & Bits
Convert between bases and inspect the bit-level representation of integers with loops.
- 14. Write a program in C to convert a binary number to decimal using a while loop.
- 15. Write a program in C to convert an octal number to decimal using a for loop.
- 16. Write a program in C to convert a decimal number to octal using a do-while loop.
- 17. Write a program in C to count the number of 1s in the binary representation of an integer using a while loop.
- 18. Write a program in C to convert a hexadecimal string to decimal using a for loop.
🔹 Advanced Patterns & Nested Loops
Interview-favourite star and number patterns built with nested for and while loops.
- 19. Write a program in C to print Pascal's triangle using nested for loops.
- 20. Write a program in C to print Floyd's triangle using nested for loops.
- 21. Write a program in C to print an X-shaped star pattern using nested for loops.
- 22. Write a program in C to print a plus-sign star pattern using nested for loops.
- 23. Write a program in C to print a number palindrome pyramid (e.g., 1, 121, 12321) using nested loops.
- 24. Write a program in C to print a full symmetric number pyramid using nested while loops.
🔹 Series Variants & Algorithms
Series computations and classic loop-driven algorithms frequently asked in interviews.
- 25. Write a program in C to calculate the sum of the series 1 + 1/2² + 1/3² + … + 1/N² using a for loop.
- 26. Write a program in C to calculate the sum of the series 1! + 2! + 3! + … + N! using a for loop.
- 27. Write a program in C to generate the first N terms of the Tribonacci sequence using a while loop.
- 28. Write a program in C to find the largest prime factor of an integer using a while loop.
- 29. Write a program in C to implement iterative binary search using a while loop (non-recursive).
- 30. Write a program in C to check whether two integers are amicable numbers using a for loop.
How to Get the Most Out of These Assignments
Reading a solution is not the same as writing one. For each assignment, follow this three-step workflow:
- Attempt first. Open a blank editor and try to solve the problem using only the assignment description. Even if you get stuck, the act of trying cements the concept.
- Compare carefully. Open the solution page and compare your approach to the reference. Look for differences in loop choice (
whilevsdo-whilevsfor), termination conditions, and edge cases like 0 and negative inputs. - Re-implement from scratch. Close the solution page and write the program again without looking. This is the step most learners skip — and it's the one that actually builds fluency.
💡 Tip: Trace every loop by hand on paper for a small input (like N = 5) before running it. Watch the loop variable and any accumulators at each iteration — most loop bugs are off-by-one errors or a missing update inside the loop body. Compile with gcc -Wall -Wextra -std=c11 -g so the compiler warns you about uninitialized variables and unreachable code.
❓ Loop Viva Questions & Quick Answers
Q1. When should I use a while loop instead of a for loop?
Use for when the number of iterations is known in advance (e.g., print the first N terms). Use while when the loop should run until a condition changes and the iteration count is unknown (e.g., Euclidean GCD, digit extraction).
Q2. What is the difference between while and do-while?
while tests the condition before the first iteration — the body may run zero times. do-while tests after the body, so it always runs at least once. This matters for input validation loops.
Q3. How do I avoid an infinite loop when extracting digits?
Make sure the update step (n /= 10) is inside the loop and that the condition (n != 0) eventually becomes false. For negative numbers, take the absolute value first or loop on n <= -1 || n >= 1.
Q4. How does the Euclidean algorithm compute GCD with a loop?
Repeatedly replace (a, b) with (b, a % b) until b becomes 0. The remaining value of a is the GCD. The repeated-subtraction variant uses a = a - b (or b = b - a) instead of the modulo.
Q5. What is the time complexity of iterative binary search?
O(log n). Each iteration halves the search range, so a sorted array of one million elements needs at most about 20 iterations.
Q6. Why do nested loops have O(n²) time complexity?
If the outer loop runs N times and, for each outer iteration, the inner loop also runs N times, the body executes N × N times. Patterns like Pascal's triangle and Floyd's triangle use nested loops but only execute the body about N²/2 times — still O(n²).