{ }codelper

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.

50 problemsLevel 1–4Solve in C

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

  1. Clap game8P0002
  2. Star triangle8P0007
  3. One row of the times table8P0022
  4. Street lamps, with one at the very end8P0371
  5. Fibonacci number8P0010
  6. Count down by steps8P0024
  7. Trail distance signs8P0414
  8. Counting places for billboards on a tile wall8P0270
  9. Multi-month calendar8P0126
  10. Vending machine change8P0127
  11. A bank ledger with commas8P0173
  12. When is the room free?8P0240
  13. Taking every few diagonal stripes of a patchwork8P0335
  14. A ruler made of characters8P0172
  15. Rewinding a work log to find the first row8P0331
  16. 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

  1. A reading marathon that grows every day8P0282
  2. Powers that fit8P0050
  3. Progressive income tax8P0137
  4. The next level keeps moving away8P0296

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

  1. Sum of digits8P0006
  2. One more throw for yut or mo8P0255
  3. Count the digits8P0052
  4. Running total until zero8P0051
  5. Prime check8P0008
  6. To binary8P0038
  7. Dice game: roll again on a 68P0264
  8. Card-drawing turns that skip rest cards8P0430
  9. Elevator log with no floor 08P0235
  10. A frog on stepping stones8P0193
  11. Santa's candy bag and the stockings8P0405
  12. Sort the digits and subtract, again and again8P0209
  13. Babylonian square root8P0130
  14. Retry with backoff8P0138
  15. Following an ATM's state8P0139
  16. 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

  1. First multiple8P0023
  2. Loading a delivery truck in order8P0223
  3. Edit a playlist8P0110

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

  1. Letter grades8P0011
  2. Counting temperature records8P0219
  3. Parking fee calculator8P0136
  4. Darts countdown game8P0140
  5. Slime Hunting Log8P0390

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

  1. Row and column sums8P0033
  2. A balance with unequal arms8P0345
  3. Table of shared free days8P0234
  4. The day with the widest swing8P0238
  5. Notebook and pen gift sets8P0353
  6. Longest increasing subsequence8P0089

Good to know

See also