Loop Programs – Full Questions
Use these as homework or practice. Each question has a sample program you can open and run.
1. Multiplication table (for loop only)
for
Question: Write a program that asks the user for one number. Using only a for loop, print the multiplication table for that number from 1 to 10 (i.e. number × 1, number × 2, … number × 10).
Use only one for loop. No while or do-while.
Example: Input 7 → Output: 7×1=7, 7×2=14, … 7×10=70
Download: next_week_for_loop.c
2. Multiplication table (while loop only)
while
Question: Write the same program as above (multiplication table from 1 to 10), but this time use only a while loop. The output must be exactly the same.
Use only one while loop. Initialize a counter before the loop and increment it inside the loop.
Example: Input 7 → Output: 7×1=7, 7×2=14, … 7×10=70
Download: next_week_while_loop.c
3. Sum of five numbers (for loop)
for
Question: Write a program that asks the user for exactly 5 numbers using a for loop. After reading all 5, print the sum of those numbers.
Use one for loop that runs 5 times. Each time, ask for a number and add it to a sum variable.
Example: Input 10, 20, 5, 3, 2 → Output: Total = 40
Download: simple_for_loop_ask.c
4. Sum until zero (while loop)
while
Question: Write a program that keeps asking the user for numbers until they enter 0. When they enter 0, stop and print the total sum of all numbers they entered (not including the 0).
Use a while loop. Condition: keep looping while the number entered is not 0. Add each number to a sum.
Example: Input 5, 10, 3, 0 → Output: Total = 18
Download: while_loop_sum_until_zero.c
5. Sum from 1 to N (for loop)
for
Question: Ask the user for a positive number N. Using a for loop, compute and print the sum 1 + 2 + 3 + … + N.
Use one for loop. Start from 1, go up to N, add each value to a sum variable.
Example: Input 5 → Output: Sum from 1 to 5 = 15
Download: loop_sum_1_to_n.c
6. Star pattern (nested for loops)
nested for
Question: Using nested for loops, print a right-angle triangle of stars: row 1 has 1 star, row 2 has 2 stars, row 3 has 3 stars, row 4 has 4 stars, row 5 has 5 stars. No input needed.
Outer loop: rows 1 to 5. Inner loop: print stars (number of stars = row number). Print a newline after each row.
Output:
*
**
***
****
*****
Download: loop_star_pattern.c
7. Count digits in a number (while loop)
while
Question: Ask the user for an integer (can be positive or negative). Using a while loop, count how many digits the number has, then print the count. (Hint: keep dividing the number by 10 until it becomes 0; count how many times you divided.)
Handle 0 as 1 digit. For negative numbers, work with the absolute value for counting.
Example: Input 1234 → Output: 1234 has 4 digit(s).
Download: loop_count_digits.c
8. Simple menu (do-while loop)
do-while
Question: Write a program that shows a menu at least once. Options: 1 – print "Hello!", 2 – print "Bye!", 0 – exit. Use a do-while loop so the menu is shown again after each choice until the user chooses 0.
Use do-while so the menu always runs at least once. Loop while choice is not 0. If user enters invalid number, print a message and show menu again.
Example: User picks 1 → "Hello!" → menu again; picks 2 → "Bye!" → menu again; picks 0 → "Goodbye!" and exit.
Download: loop_do_while_menu.c
9. Countdown from N to 1 (for loop)
for
Question: Ask the user for a number N. Using a for loop, print a countdown: N, N-1, N-2, … 1, then print "Done!".
Use a for loop that starts at N and decrements (i--) until 1.
Example: Input 4 → Output: Countdown: 4 3 2 1 Done!
Download: loop_reverse_count_for.c
10. Factorial of N (for loop)
for
Question: Ask the user for a non-negative integer N. Compute and print N! (factorial). N! = 1 × 2 × 3 × … × N. (Example: 5! = 120.)
Use a for loop. Start with a variable 1, multiply by 1, then 2, then 3, … up to N. Handle N=0 (0! = 1) and negative (print a message).
Example: Input 5 → Output: 5! = 120
Download: loop_factorial_for.c
11. Ask until valid (while loop)
while
Question: Ask the user to enter a number between 1 and 10. If they enter a number outside this range, keep asking until they enter a valid number. Then print "You entered: X".
Use a while loop. Condition: while the number is less than 1 OR greater than 10, ask again.
Example: User types 15 → "Invalid! Must be 1 to 10. Try again:" → types 3 → "You entered: 3"
Download: loop_ask_until_valid.c
12. Math practice game (mixed loops – challenge)
do-while, for, while
Question: Write a simple math quiz game. Show a menu with: Play (1), Quit (0). Use do-while for the main menu. When Play is chosen, use a for loop to ask 5 math questions (e.g. two random numbers, user types the sum). If the user types invalid input, use a while loop to ask again. Use break to exit when user chooses Quit.
Combine do-while (menu), for (5 questions), and while (input validation). Optional: use break to quit.
Menu → 1 → 5 questions → show score → menu again. Menu → 0 → goodbye.
Download: math_practice_game.c