Here are 13 problems, from beginner to advanced, that use for and while loops. 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.
"Run" executes your code with the input above. "Check" tests it against 4 prepared inputs.
sum that holds the total to 0, then repeat sum += i; with for (i = 1; i <= n; i++).#include <stdio.h> int main(void) { int n; scanf("%d", &n); int sum = 0; for (int i = 1; i <= n; i++) { sum += i; } printf("Sum: %d\n", sum); return 0; }
sum, you start adding to an indeterminate value. If the condition is i < n, N is not added (one short — an "off-by-one" error).%3d so that it is 3 characters wide. There is no input."Run" executes your code with the input above. "Check" tests it against 1 prepared inputs.
printf("\n");.#include <stdio.h> int main(void) { for (int i = 1; i <= 9; i++) { for (int j = 1; j <= 9; j++) { printf("%3d", i * j); } printf("\n"); } return 0; }
printf("\n") inside the inner loop, a newline is printed after every number."Run" executes your code with the input above. "Check" tests it against 4 prepared inputs.
i at 2 and increase it with i += 2, you get only the even numbers in order. You can also solve it by testing i % 2 == 0.#include <stdio.h> int main(void) { int n; scanf("%d", &n); for (int i = 2; i <= n; i += 2) { printf("%d ", i); } printf("\n"); return 0; }
i++; you can write anything, such as i += 2. This checker ignores trailing spaces, so it is still correct even though "%d " leaves a space at the end of the line.N! = value". 0! is 1."Run" executes your code with the input above. "Check" tests it against 4 prepared inputs.
long long type and the format %lld.#include <stdio.h> int main(void) { int n; scanf("%d", &n); long long fact = 1; for (int i = 1; i <= n; i++) { fact *= i; } printf("%d! = %lld\n", n, fact); return 0; }
"Run" executes your code with the input above. "Check" tests it against 4 prepared inputs.
n % 10 gives the ones digit, and n / 10 drops the ones digit. Repeat this with a while loop until n becomes 0.#include <stdio.h> int main(void) { int n; scanf("%d", &n); int sum = 0; while (n > 0) { sum += n % 10; n /= 10; } printf("Sum of digits: %d\n", sum); return 0; }
No data"."Run" executes your code with the input above. "Check" tests it against 4 prepared inputs.
while (1) and leave it with break when the value read is 0. For the average, divide with one side converted to double, as in (double)sum / count.#include <stdio.h> int main(void) { int x, count = 0, sum = 0; while (1) { scanf("%d", &x); if (x == 0) break; count++; sum += x; } if (count == 0) { printf("No data\n"); } else { printf("Count: %d\n", count); printf("Sum: %d\n", sum); printf("Average: %.2f\n", (double)sum / count); } return 0; }
sum / count, it is division of two ints and the fractional part is discarded. Worse, that int value is then passed to %.2f, so the output is garbage (undefined behavior; some environments print 0.00). The compiler warns about this too, so always read the warnings. Also, dividing while count is still 0 is division by zero, so handle that case separately first."Run" executes your code with the input above. "Check" tests it against 4 prepared inputs.
i from 1 to N, if n % i == 0 (it divides evenly), i is a divisor. Count them while you print them.#include <stdio.h> int main(void) { int n; scanf("%d", &n); int count = 0; for (int i = 1; i <= n; i++) { if (n % i == 0) { printf("%d ", i); count++; } } printf("\nNumber of divisors: %d\n", count); return 0; }
i <= n.*. Row i has i * characters."Run" executes your code with the input above. "Check" tests it against 3 prepared inputs.
i from 1 to N, and the inner loop prints i * characters. The key point is that the inner loop's end condition depends on the outer variable i.#include <stdio.h> int main(void) { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { printf("*"); } printf("\n"); } return 0; }
* characters, so the inner loop uses j <= i.Fizz for multiples of 3, Buzz for multiples of 5, and FizzBuzz for multiples of both 3 and 5."Run" executes your code with the input above. "Check" tests it against 4 prepared inputs.
#include <stdio.h> int main(void) { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { if (i % 15 == 0) { printf("FizzBuzz\n"); } else if (i % 3 == 0) { printf("Fizz\n"); } else if (i % 5 == 0) { printf("Buzz\n"); } else { printf("%d\n", i); } } return 0; }
i % 3 == 0 first, 15 also stops at "Fizz" and FizzBuzz never appears. When conditions overlap, write the stricter condition first."Run" executes your code with the input above. "Check" tests it against 5 prepared inputs.
k from 2 to N, and the inner loop checks "is it divisible by any number from 2 to √k". Using j * j <= k as the condition avoids computing √.#include <stdio.h> int main(void) { int n; scanf("%d", &n); int count = 0; for (int k = 2; k <= n; k++) { int is_prime = 1; for (int j = 2; j * j <= k; j++) { if (k % j == 0) { is_prime = 0; break; } } if (is_prime) { printf("%d ", k); count++; } } printf("\nNumber of primes: %d\n", count); return 0; }
is_prime) to 1 for each candidate. Using break as soon as a divisor is found avoids wasted work.*. Row i consists of N−i spaces followed by 2i−1 * characters (no trailing spaces)."Run" executes your code with the input above. "Check" tests it against 3 prepared inputs.
n - i spaces, the second prints 2 * i - 1 * characters, and then you print a newline.#include <stdio.h> int main(void) { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { for (int j = 0; j < n - i; j++) { printf(" "); } for (int j = 0; j < 2 * i - 1; j++) { printf("*"); } printf("\n"); } return 0; }
"Run" executes your code with the input above. "Check" tests it against 4 prepared inputs.
(a, b) with (b, a % b) until b becomes 0". The a at that point is the GCD. The LCM is a / g * b (where g is the GCD). The result may not fit in an int, so compute (long long)a / g * b and print it with %lld.#include <stdio.h> int main(void) { int a, b; scanf("%d %d", &a, &b); int x = a, y = b; while (y != 0) { int r = x % y; x = y; y = r; } int g = x; long long l = (long long)a / g * b; printf("GCD: %d\n", g); printf("LCM: %lld\n", l); return 0; }
a * b / g, the multiplication is likely to overflow, so divide first."Run" executes your code with the input above. "Check" tests it against 4 prepared inputs.
while (n != 1), and each time do steps++ and update the maximum. The values along the way can become hundreds of times larger than N, so it is safer to use long long.#include <stdio.h> int main(void) { long long n; scanf("%lld", &n); int steps = 0; long long max = n; while (n != 1) { if (n % 2 == 0) { n /= 2; } else { n = 3 * n + 1; } steps++; if (n > max) max = n; } printf("Steps: %d\n", steps); printf("Max: %lld\n", max); return 0; }