{ }codelper

100 C Programming Practice Problems for Beginners

These 100 problems follow the order a C course teaches things in. You don't need to install a compiler: this site compiles your code with GCC and checks it.

100 problemsLevel 1–2Solve in C

1. Input and output #1–9

With scanf, put & in front of the variable to pass its address (character arrays are the only exception).
The format specifier has to match the type: %d for int, %lld for long long, and for double, %lf in scanf and %f in printf.

Syntax refresher: C scanf(): Read Input · C printf(): Format Specifiers and Output · C scanf String Input: %s and fgets

  1. Copying a guestbook line8P0311
  2. Sum of two numbers8P0001
  3. Bonus-added scores joined with commas8P0373
  4. Numbering the lines of a song8P0347
  5. Score statistics8P0003
  6. Exactly the money you have8P0043
  7. Sum to the end8P0018
  8. Counting paragraphs split by empty lines8P0438
  9. Weight on the scale8P0221

2. Variables and types #10–21

An int only holds values up to about ±2.1 billion.
If a number could be bigger, use long long.
Overflow gives no error, just a wrong value, so it is hard to track down.
A local variable holds a garbage value until you give it one.

  1. Marbles passed in turn8P0251
  2. The shorter one stands in front8P0344
  3. Four lines of true and false8P0217
  4. Ticket number from a seat code8P0226
  5. Sale price, or list price if there is none8P0281
  6. Swap the cards8P0046
  7. Big sum8P0015
  8. Truncate, floor, round8P0044
  9. A siren that keeps sounding until switched off8P0289
  10. Letter chain8P0045
  11. Ride capacity8P0086
  12. Three hops to the power box contact8P0286

3. Arithmetic #22–34

/ between two integers drops everything after the decimal point (7 / 2 is 3).
If you need a fraction, convert one side to double.
The result of % takes the sign of the number being divided, so -7 % 2 is -1.

  1. Distance per liter8P0216
  2. Sum of digits8P0006
  3. How many combinations a luggage lock has8P0423
  4. Grid distance8P0017
  5. Grading addition homework8P0222
  6. Seconds to H:MM:SS8P0013
  7. Free-throw success rate sheet8P0314
  8. Weekday after k days8P0016
  9. Round half to even8P0048
  10. This year's savings goal rate8P0407
  11. Powers that fit8P0050
  12. Closest to the target8P0218
  13. Where is the square root8P0049

4. Conditionals #35–45

== compares and = assigns.
if (x = 5) compiles, but it is always true.
In a switch, a case without break keeps running into the branches below it.

Syntax refresher: C if Statement: if, else if, else · C switch Statement: case, break, default

  1. A photo fitted to a square frame8P0254
  2. Letter grades8P0011
  3. Leap year8P0019
  4. Locker extra fee8P0372
  5. Three-day temperature trend8P0227
  6. Member discount and delivery fee8P0191
  7. Kite-flying notice board8P0315
  8. Middle of three8P0020
  9. Four-operation calculator8P0021
  10. Password rule checker8P0098
  11. Due dates that skip closed days8P0431

5. Loops #46–58

for (int i = 0; i < n; i++) is the basic form.
Mix up < and <= and the loop runs once too many or once too few.
When the body has to run at least once, use do { } while (condition);, and don't forget the semicolon at the end.

Syntax refresher: C for Loop · C while Loop and do-while

  1. Clap game8P0002
  2. Star triangle8P0007
  3. One row of the times table8P0022
  4. One more throw for yut or mo8P0255
  5. First multiple8P0023
  6. Street lamps, with one at the very end8P0371
  7. A reading marathon that grows every day8P0282
  8. Fibonacci number8P0010
  9. Counting temperature records8P0219
  10. Count the digits8P0052
  11. Loading a delivery truck in order8P0223
  12. Count down by steps8P0024
  13. Running total until zero8P0051

6. Arrays #59–70

An array of size n has indexes 0 to n-1.
C does not stop you from going out of bounds, so writing past the end can break another variable or cause a Segmentation fault.
You cannot copy one array to another with =, so move the elements one by one in a loop.

Syntax refresher: C Arrays: Declare, Initialize, and Loop

  1. Largest number and its position8P0005
  2. Numbers of the students who retake8P0374
  3. Showing one page of a board8P0284
  4. Sort the numbers8P0036
  5. Sorting sign letters8P0253
  6. Is it on the list?8P0059
  7. Swapping places in pairs8P0312
  8. Travel time on a one-way bus route8P0224
  9. Remove every match8P0058
  10. Rotate a list right8P0032
  11. Row and column sums8P0033
  12. Number order and dictionary order8P0320

7. Strings #71–82

A C string is a char array with '\0' at the end.
To read 100 characters you need char s[101].
Compare contents with strcmp, not ==, and get the length with strlen.

Syntax refresher: C Strings: char Arrays, strlen, strcpy, strcmp · C scanf String Input: %s and fgets

  1. Count the vowels8P0009
  2. First and last position of a character8P0053
  3. Letters picked along the diagonal8P0283
  4. Count the words8P0012
  5. Palindrome check8P0004
  6. Reverse it, keep the sign8P0252
  7. Swap the case8P0025
  8. A price tag with dots and commas swapped8P0412
  9. Characters left in a message with Korean text8P0341
  10. Letter numbers in a note8P0192
  11. Cut from the a-th to the b-th character8P0054
  12. Reverse the word order8P0028

8. Sum, average, maximum #83–87

Always start a sum variable at 0.
If there are many numbers or they are large, keep the sum in a long long.
To get the average as a fraction, convert before you divide, as in (double)sum / n.

  1. Dart scores thrown in turns8P0342
  2. Mornings at or above average8P0343
  3. Second largest8P0031
  4. Class averages and the year average8P0225
  5. The day closest to the average8P0418

9. Functions #88–95

Define a function before you use it, or write its prototype near the top.
A function that returns a value has to return on every branch.
Arguments are passed as copies, so changing one inside the function leaves the outside variable as it was.

  1. A thermometer that cannot leave its scale8P0402
  2. Balls that landed in the sandbox8P0285
  3. Prime check8P0008
  4. A running course made of three flags8P0435
  5. A box-count function with defaults8P0257
  6. Tidy locker numbers8P0196
  7. Hall chairs numbered in a zigzag8P0428
  8. Pick the top word with a key function passed in8P0318

10. A first look at pointers #96–100

&x is the address of x, and *p is the value at the place p points to.
To let a function change an outside variable, pass the address and write to *p.
Swapping two values is the best first exercise.

Syntax refresher: C Pointers Explained for Beginners

  1. Min, max and sum through pointers2P0073
  2. Walking to the longest run with pointers2P0074
  3. Keeping the words2P0075
  4. Name tags pointing at the same list8P0262
  5. The robot that bounces back3P0203

Good to know

See also