C Loop Practice Problems: for, while, do-while
This set is loops only. It has 50 problems, from running a for a set number of times to while, do-while and nested loops.
1. Repeating a set number of times with for #1–16
for (int i = 0; i < n; i++) runs n times.
Write i <= n and it runs one more time.
To count down, use for (int i = n; i >= 1; i--).
To skip ahead several at a time, change the update, as in i += 2.
Syntax refresher: C for Loop
- Clap game8P0002
- Star triangle8P0007
- One row of the times table8P0022
- Street lamps, with one at the very end8P0371
- Fibonacci number8P0010
- Count down by steps8P0024
- Trail distance signs8P0414
- Counting places for billboards on a tile wall8P0270
- Multi-month calendar8P0126
- Vending machine change8P0127
- A bank ledger with commas8P0173
- When is the room free?8P0240
- Taking every few diagonal stripes of a patchwork8P0335
- A ruler made of characters8P0172
- Rewinding a work log to find the first row8P0331
- Planning adventure gold backwards8P0436
2. Adding and counting as you go #17–20
Start the variable that holds a sum or a count at 0 outside the loop.
If you create it inside, it goes back to 0 on every pass.
If the sum could go past 2.1 billion, keep it in a long long.
Syntax refresher: C for Loop
3. while and do-while #21–36
Use while when you don't know how many times the loop will run.
To read until the input ends, use the value scanf returns as the condition, as in while (scanf("%d", &x) == 1).do { } while (condition); checks the condition afterwards, so the body always runs at least once.
Syntax refresher: C while Loop and do-while
- Sum of digits8P0006
- One more throw for yut or mo8P0255
- Count the digits8P0052
- Running total until zero8P0051
- Prime check8P0008
- To binary8P0038
- Dice game: roll again on a 68P0264
- Card-drawing turns that skip rest cards8P0430
- Elevator log with no floor 08P0235
- A frog on stepping stones8P0193
- Santa's candy bag and the stockings8P0405
- Sort the digits and subtract, again and again8P0209
- Babylonian square root8P0130
- Retry with backoff8P0138
- Following an ATM's state8P0139
- Redact secrets in a log8P0171
4. break and continue #37–39
break ends the loop right away, and continue skips the rest of the current pass.
In nested loops, break ends only the innermost one.
Syntax refresher: C while Loop and do-while
5. Going through an array in order #40–44
C has no loop that hands you the elements one at a time, so you loop over the indexes.
An array does not tell you its own length either, so you have to carry the element count n around separately.
Syntax refresher: C Arrays: Declare, Initialize, and Loop
6. Nested loops #45–50
Each time the outer loop runs once, the inner loop runs from start to finish.
Multiplication tables, star patterns and printed tables are the typical problems.
Check that you have not mixed up the inner and outer variable names (i, j).
Syntax refresher: C for Loop
Good to know
- If you put a semicolon after the loop (
for (...);), it repeats an empty statement and the code in the braces runs only once. - If you get time limit exceeded instead of an answer, check that the variable in the condition actually changes inside the loop.
See also
- C if-else Practice Problems — 42 problems
- C Array and String Practice Problems — 81 problems
- 100 C Programming Practice Problems for Beginners — 100 problems
- Learn C — lessons with explanations and examples
- C reference
- C error messages
- Other problem sets