C Variables, Operators & I/O Exercises (printf & scanf)

Here are 13 problems, from beginner to advanced, that use variables, arithmetic operators and printf / scanf. Write your code in the browser and press "Check": it is run with real gcc on several inputs to see whether the output is correct.

How to use these problems
  1. Read the problem and the sample run, then write your code in the box
  2. Press "▶ Run" to try it with your own input
  3. Press "✔ Check" to test the output against several prepared inputs
  4. Stuck? Open "Hint". Solved it? Compare with "Solution & explanation"
Checking rules
  • Print output exactly as in the sample run. Do not print input prompts such as "Enter a number"
  • Trailing spaces at line ends and trailing blank lines are ignored
  • Your code is saved automatically in this browser

Note: Run and Check send your code to Wandbox, a free external compiler service, and run it with gcc.

13 problems (Beginner 4 · Intermediate 5 · Advanced 4)

Variables, operators & I/O (13 problems)

B01 Beginner

Problem 1. The four arithmetic operations on two integers

Read two integers a and b (−10000 ≤ a, b ≤ 10000, b ≠ 0) separated by a space, and print their sum, difference (a−b), product, quotient and remainder, one per line, as in the example. For the quotient and remainder, print the results of C's / and % as they are.
Sample run (input is underlined)
17 5 Sum: 22 Difference: 12 Product: 85 Quotient: 3 Remainder: 2
Your code
"Run" executes your code with the input above. "Check" tests it against 5 prepared inputs.
With two ints, a / b is integer division (the fractional part is discarded) and a % b is the remainder. You can print the results directly, e.g. printf("Quotient: %d\n", a / b);.
#include <stdio.h>

int main(void) {
    int a, b;
    scanf("%d %d", &a, &b);

    printf("Sum: %d\n", a + b);
    printf("Difference: %d\n", a - b);
    printf("Product: %d\n", a * b);
    printf("Quotient: %d\n", a / b);
    printf("Remainder: %d\n", a % b);
    return 0;
}
Explanation: Integer division in C truncates toward 0. So −7 / 2 is −3, not −4, and the remainder is chosen so that a == (a / b) * b + a % b holds, which makes it −1 (the remainder has the same sign as a). Also, with scanf, don't forget the &, as in &a.
B02 Beginner

Problem 2. Area and circumference of a circle

Read the radius r of a circle (0 < r ≤ 1000, may have a fractional part) and print its area and circumference to 2 decimal places. Use 3.14159 for pi.
Sample run (input is underlined)
2.5 Area: 19.63 Circumference: 15.71
Your code
"Run" executes your code with the input above. "Check" tests it against 5 prepared inputs.
Since you are working with decimals, use the double type. In scanf use %lf; to print 2 decimal places with printf use %.2f. The area is PI * r * r and the circumference is 2 * PI * r.
#include <stdio.h>

int main(void) {
    const double PI = 3.14159;
    double r;
    scanf("%lf", &r);

    printf("Area: %.2f\n", PI * r * r);
    printf("Circumference: %.2f\n", 2 * PI * r);
    return 0;
}
Explanation: Writing scanf("%f", &r) to read a double is wrong (%f is for float). Remember: %lf for scanf, %f for printf. %.2f rounds at the 3rd decimal place and shows 2 decimal places (the variable's value itself does not change). However, because a double is a binary approximation, a value exactly at a "5" may not round up — for example, 2.675 becomes 2.67.
B03 Beginner

Problem 3. Celsius to Fahrenheit

Read a temperature C in Celsius (−100 ≤ C ≤ 1000, may have a fractional part) and print the Fahrenheit temperature F = C × 9 / 5 + 32 to 1 decimal place.
Sample run (input is underlined)
36.6 Fahrenheit: 97.9
Your code
"Run" executes your code with the input above. "Check" tests it against 5 prepared inputs.
If you write the formula as is, c * 9 / 5 + 32, the calculation is done in double because c is a double. Be careful with ways of writing it that compute 9 / 5 first.
#include <stdio.h>

int main(void) {
    double c;
    scanf("%lf", &c);

    double f = c * 9 / 5 + 32;
    printf("Fahrenheit: %.1f\n", f);
    return 0;
}
Explanation: If you write c * (9 / 5) + 32, 9 / 5 is integer division and gives 1, so you get the wrong formula F = C + 32. Either write one of the numbers as a decimal, like 9.0 / 5, or multiply by c first (left to right) so the value is a double before dividing.
B04 Beginner

Problem 4. Digits of a 3-digit number and its reverse

Read a 3-digit integer N (100 ≤ N ≤ 999) and print its hundreds, tens and ones digits. Then print the number formed by reversing the digits as an integer (e.g. 21 for 120). You don't need a loop.
Sample run (input is underlined)
123 Hundreds: 1 Tens: 2 Ones: 3 Reversed: 321
Your code
"Run" executes your code with the input above. "Check" tests it against 4 prepared inputs.
The hundreds digit is n / 100, the tens digit is n / 10 % 10, and the ones digit is n % 10. The reversed number is ones * 100 + tens * 10 + hundreds.
#include <stdio.h>

int main(void) {
    int n;
    scanf("%d", &n);

    int h = n / 100;
    int t = n / 10 % 10;
    int o = n % 10;

    printf("Hundreds: %d\n", h);
    printf("Tens: %d\n", t);
    printf("Ones: %d\n", o);
    printf("Reversed: %d\n", o * 100 + t * 10 + h);
    return 0;
}
Explanation: Think of / as "drop the lower digits" and % as "keep only the lower digits", and you can extract any digit. If you print the reverse as three separate numbers with printf("%d%d%d", o, t, h), 120 gives 021, which is not the integer 21.
B05 Intermediate

Problem 5. Total and average of 3 subjects

Read the scores of 3 subjects (integers from 0 to 100) separated by spaces, and print the total and the average. Print the average to 2 decimal places.
Sample run (input is underlined)
80 75 91 Total: 246 Average: 82.00
Your code
"Run" executes your code with the input above. "Check" tests it against 5 prepared inputs.
Compute the total as an int. For the average, don't use sum / 3; use (double)sum / 3 or sum / 3.0 so that the division keeps the fractional part.
#include <stdio.h>

int main(void) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);

    int sum = a + b + c;
    double avg = (double)sum / 3;

    printf("Total: %d\n", sum);
    printf("Average: %.2f\n", avg);
    return 0;
}
Explanation: Even if you write double avg = sum / 3;, the right-hand side is division of two ints, so the fraction is discarded first and 299 / 3 becomes 99 → 99.00. The key is to convert the type (cast) with (double) before dividing. Writing (double)(sum / 3) with parentheses is pointless, because it converts after the truncation.
B06 Intermediate

Problem 6. Convert seconds to h:mm:ss

Read a number of seconds N (0 ≤ N ≤ 360000) and print it in the form "hours:minutes:seconds". Minutes and seconds use 2 digits (pad with a leading 0 when they are a single digit); print the hours as they are, without padding.
Sample run (input is underlined)
3725 1:02:05
Your code
"Run" executes your code with the input above. "Check" tests it against 5 prepared inputs.
Hours are n / 3600, minutes are n % 3600 / 60, and seconds are n % 60. To print 2 digits padded with 0, use %02d.
#include <stdio.h>

int main(void) {
    int n;
    scanf("%d", &n);

    int h = n / 3600;
    int m = n % 3600 / 60;
    int s = n % 60;

    printf("%d:%02d:%02d\n", h, m, s);
    return 0;
}
Explanation: If you compute the minutes as n / 60, 3725 seconds gives 62 minutes, including the minutes that already carried over into hours. First take % 3600 to get "what remains below one hour", then divide by 60. %2d pads with spaces and gives 1: 2: 5; to pad with 0, use %02d.
B07 Intermediate

Problem 7. Break an amount into bills and coins

Read an amount of N yen (0 ≤ N ≤ 100000). Using 1000-yen bills and 500-, 100-, 50-, 10-, 5- and 1-yen coins, pay it so that the total number of bills and coins is as small as possible, and print how many of each you use, from largest to smallest, in the format of the example.
Sample run (input is underlined)
1888 1000 yen: 1 500 yen: 1 100 yen: 3 50 yen: 1 10 yen: 3 5 yen: 1 1 yen: 3
Your code
"Run" executes your code with the input above. "Check" tests it against 4 prepared inputs.
Starting from the largest denomination, find the count with n / 1000, then make n the remaining amount with n %= 1000;. Continue the same way with 500, 100, and so on.
#include <stdio.h>

int main(void) {
    int n;
    scanf("%d", &n);

    printf("1000 yen: %d\n", n / 1000);
    n %= 1000;
    printf("500 yen: %d\n", n / 500);
    n %= 500;
    printf("100 yen: %d\n", n / 100);
    n %= 100;
    printf("50 yen: %d\n", n / 50);
    n %= 50;
    printf("10 yen: %d\n", n / 10);
    n %= 10;
    printf("5 yen: %d\n", n / 5);
    n %= 5;
    printf("1 yen: %d\n", n);
    return 0;
}
Explanation: This is the classic combination of / and %: "the quotient is the count, and the remainder is the amount passed on to the next step". n %= 1000; is shorthand (compound assignment) for n = n % 1000;. If you forget to update the remaining amount, you get strange results such as 18 hundred-yen coins for 1888 yen.
B08 Intermediate

Problem 8. Character codes and upper/lower case

Read one uppercase letter (A to Z) and print its character code (ASCII code), the corresponding lowercase letter, and its position in the alphabet (A is position 1), in the format of the example.
Sample run (input is underlined)
C Code: 67 Lowercase: c Position: 3
Your code
"Run" executes your code with the input above. "Check" tests it against 4 prepared inputs.
A char is a small integer: print it with %d to get the character code, or with %c to get the character. The lowercase letter is c - 'A' + 'a', and the position is c - 'A' + 1.
#include <stdio.h>

int main(void) {
    char c;
    scanf(" %c", &c);

    printf("Code: %d\n", c);
    printf("Lowercase: %c\n", c - 'A' + 'a');
    printf("Position: %d\n", c - 'A' + 1);
    return 0;
}
Explanation: 'A' is an integer representing the character code (65 in ASCII). In ASCII, A–Z and a–z are each laid out consecutively, so you can convert by adding or subtracting "how far it is from A". Writing 'a' - 'A' conveys your intent better than writing a number like 32 directly.
B09 Intermediate

Problem 9. Integer part, fractional part and rounding

Read a non-negative decimal x (0 ≤ x < 100000, up to 2 decimal places) and print its integer part, its fractional part (to 2 decimal places), and the integer obtained by rounding at the first decimal place. Use casts (type conversion), not functions from math.h.
Sample run (input is underlined)
3.7 Integer part: 3 Fractional part: 0.70 Rounded: 4
Your code
"Run" executes your code with the input above. "Check" tests it against 5 prepared inputs.
(int)x discards the fractional part and gives the integer part. The fractional part is x - (int)x. Rounding is "add 0.5, then truncate", i.e. (int)(x + 0.5).
#include <stdio.h>

int main(void) {
    double x;
    scanf("%lf", &x);

    int ip = (int)x;
    double fp = x - ip;
    int rounded = (int)(x + 0.5);

    printf("Integer part: %d\n", ip);
    printf("Fractional part: %.2f\n", fp);
    printf("Rounded: %d\n", rounded);
    return 0;
}
Explanation: Converting a double to an int truncates toward 0. If you write (int)x + 0.5, it truncates first and then adds 0.5, so it does not round (watch where the parentheses go). Note that adding 0.5 only works when x is 0 or more; for negative numbers, −2.7 would become −2.
B10 Advanced

Problem 10. BMI and standard weight

Read a weight w [kg] (may have a fractional part) and a height h [cm] (an integer) separated by a space, and print BMI = weight ÷ (height [m])2 and standard weight = 22 × (height [m])2 to 1 decimal place (30 ≤ w ≤ 200, 100 ≤ h ≤ 250).
Sample run (input is underlined)
60 170 BMI: 20.8 Standard weight: 63.6kg
Your code
"Run" executes your code with the input above. "Check" tests it against 5 prepared inputs.
First convert the height to meters. Since h is an int, h / 100 would be integer division. Use h / 100.0 and store the result in a double.
#include <stdio.h>

int main(void) {
    double w;
    int h;
    scanf("%lf %d", &w, &h);

    double m = h / 100.0;
    double bmi = w / (m * m);
    double std = 22 * m * m;

    printf("BMI: %.1f\n", bmi);
    printf("Standard weight: %.1fkg\n", std);
    return 0;
}
Explanation: With double m = h / 100;, 170 / 100 becomes 1 by integer division before it is assigned to the double, so m = 1.0. The trap is that even when the destination is a double, the right-hand side is computed in the types of the right-hand side. Also, w / m * m means (w / m) * m, which gives back roughly w, so you need parentheses.
B11 Advanced

Problem 11. Make a receipt

Read "unit price quantity" on each of 3 lines (unit price is an integer from 1 to 99999, quantity from 1 to 99) and print a receipt as in the example. Print each item line with the format printf("%5d x %2d = %6d\n", price, quantity, amount). Then print the Subtotal, the Tax (10% of the subtotal, fractions of a yen discarded) and the Total, each in the form "Subtotal: %d\n".
Sample run (input is underlined)
120 3 1980 1 85 12 120 x 3 = 360 1980 x 1 = 1980 85 x 12 = 1020 Subtotal: 3360 Tax: 336 Total: 3696
Your code
"Run" executes your code with the input above. "Check" tests it against 4 prepared inputs.
%5d means "right-aligned in a field at least 5 characters wide". If you compute the tax as an int with sub * 10 / 100, integer division discards fractions of a yen.
#include <stdio.h>

int main(void) {
    int p1, q1, p2, q2, p3, q3;
    scanf("%d %d", &p1, &q1);
    scanf("%d %d", &p2, &q2);
    scanf("%d %d", &p3, &q3);

    int s1 = p1 * q1;
    int s2 = p2 * q2;
    int s3 = p3 * q3;
    printf("%5d x %2d = %6d\n", p1, q1, s1);
    printf("%5d x %2d = %6d\n", p2, q2, s2);
    printf("%5d x %2d = %6d\n", p3, q3, s3);

    int sub = s1 + s2 + s3;
    int tax = sub * 10 / 100;
    printf("Subtotal: %d\n", sub);
    printf("Tax: %d\n", tax);
    printf("Total: %d\n", sub + tax);
    return 0;
}
Explanation: A width is a minimum width, so a value longer than 6 digits, such as 9899901, is printed in full without being cut (the 3rd test). If you compute with decimals, like sub * 0.1, the type no longer matches %d when printing. Keep money as int and multiply before dividing — that is the safe way (in the order sub / 100 * 10, 1343 yen gives 130 yen).
B12 Advanced

Problem 12. Division and remainder of negative numbers

Read an integer a (−10000 ≤ a ≤ 10000) and a positive integer b (1 ≤ b ≤ 10000), and print the values of C's a / b and a % b, the value of (a / b) * b + a % b as a check, and the mathematical remainder (the remainder that is at least 0 and less than b).
Sample run (input is underlined)
-7 3 Quotient: -2 Remainder: -1 Check: -7 Math remainder: 2
Your code
"Run" executes your code with the input above. "Check" tests it against 5 prepared inputs.
C's % returns a negative remainder when a is negative. Add b to it and take the remainder by b once more — (a % b + b) % b — to get a remainder that is always 0 or more.
#include <stdio.h>

int main(void) {
    int a, b;
    scanf("%d %d", &a, &b);

    int q = a / b;
    int r = a % b;
    int m = (r + b) % b;

    printf("Quotient: %d\n", q);
    printf("Remainder: %d\n", r);
    printf("Check: %d\n", q * b + r);
    printf("Math remainder: %d\n", m);
    return 0;
}
Explanation: Since C99, integer division is defined to truncate toward 0 (−7 / 3 is −2.33… → −2). The remainder is chosen so that (a / b) * b + a % b == a holds, so it is −1. When you handle negative values in day-of-week or clock calculations, r + b alone gives b when r = 0, so take % b once more at the end.
B13 Advanced

Problem 13. Adding time

Read the starting hour h (0–23), minute m (0–59) and an elapsed time of t minutes (0 ≤ t ≤ 100000) separated by spaces, and print the time t minutes later as HH:MM (both 2 digits, zero-padded, 24-hour clock). On the next line, print how many days the date has advanced.
Sample run (input is underlined)
23 30 90 01:00 +1 day(s)
Your code
"Run" executes your code with the input above. "Check" tests it against 5 prepared inputs.
Don't handle hours and minutes separately. First convert everything to "total minutes since 0:00", h * 60 + m + t. From that, the number of days is / 1440 and the minutes within the day are % 1440.
#include <stdio.h>

int main(void) {
    int h, m, t;
    scanf("%d %d %d", &h, &m, &t);

    int total = h * 60 + m + t;
    int days = total / 1440;
    int rest = total % 1440;

    printf("%02d:%02d\n", rest / 60, rest % 60);
    printf("+%d day(s)\n", days);
    return 0;
}
Explanation: If you carry over by hand — add 45 to the minutes, and if it goes past 60 add 1 to the hour, and so on — you need more if statements and it is easy to make mistakes. First convert everything to the smallest unit (minutes), then split it up with / and %: that is the standard technique for times and unit conversions (1 day = 24 × 60 = 1440 minutes).

Review these lessons

← Back to all practice problems