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.
Note: Run and Check send your code to Wandbox, a free external compiler service, and run it with gcc.
/ and % as they are."Run" executes your code with the input above. "Check" tests it against 5 prepared inputs.
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; }
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."Run" executes your code with the input above. "Check" tests it against 5 prepared inputs.
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; }
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."Run" executes your code with the input above. "Check" tests it against 5 prepared inputs.
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; }
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."Run" executes your code with the input above. "Check" tests it against 4 prepared inputs.
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; }
/ 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."Run" executes your code with the input above. "Check" tests it against 5 prepared inputs.
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; }
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."Run" executes your code with the input above. "Check" tests it against 5 prepared inputs.
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; }
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."Run" executes your code with the input above. "Check" tests it against 4 prepared inputs.
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; }
/ 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.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."Run" executes your code with the input above. "Check" tests it against 4 prepared inputs.
%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; }
'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."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; }
(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."Run" executes your code with the input above. "Check" tests it against 5 prepared inputs.
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; }
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.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"."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; }
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).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)."Run" executes your code with the input above. "Check" tests it against 5 prepared inputs.
% 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; }
(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.HH:MM (both 2 digits, zero-padded, 24-hour clock). On the next line, print how many days the date has advanced."Run" executes your code with the input above. "Check" tests it against 5 prepared inputs.
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; }
/ and %: that is the standard technique for times and unit conversions (1 day = 24 × 60 = 1440 minutes).