Lesson Progress
0%
Practice Set  ·  Number Logic & Loops

9 Classic Practice Programs Every C Learner Writes

Armstrong numbers, Strong numbers, Fibonacci, primes, GCD, LCM, and ASCII printing — the number-and-loop exercises that show up in every C course and interview.

Number checks
Series & primes
GCD & LCM
ASCII printing
program 1
1

Armstrong Number

A number equals the sum of its own digits, each raised to the power of the digit count. 153 = 1³+5³+3³.
armstrong.c
armstrong.c
C
#include <stdio.h>
#include <math.h>

int main() {
    int n, temp, digits = 0, sum = 0, rem;
    printf("Enter number: "); scanf("%d", &n);
    temp = n;

    while (temp != 0) { temp /= 10; digits++; }   // count digits

    temp = n;
    while (temp != 0) {
        rem = temp % 10;
        sum += pow(rem, digits);
        temp /= 10;
    }

    printf(sum == n ? "%d is an Armstrong number\n" : "%d is NOT an Armstrong number\n", n);
    return 0;
}
terminal
output
Enter number: 153
153 is an Armstrong number
program 2
2

Strong Number

A number equals the sum of the factorials of its digits. 145 = 1! + 4! + 5!.
strong_number.c
strong_number.c
C
#include <stdio.h>

int factorial(int n) { return n <= 1 ? 1 : n * factorial(n - 1); }

int main() {
    int n, temp, sum = 0, rem;
    printf("Enter number: "); scanf("%d", &n);
    temp = n;

    while (temp != 0) {
        rem = temp % 10;
        sum += factorial(rem);
        temp /= 10;
    }

    printf(sum == n ? "%d is a Strong number\n" : "%d is NOT a Strong number\n", n);
    return 0;
}
terminal
output
Enter number: 145
145 is a Strong number
program 3
3

Fibonacci Series

Each number is the sum of the previous two: 0, 1, 1, 2, 3, 5, 8...
fibonacci_series.c
fibonacci_series.c
C
#include <stdio.h>

int main() {
    int n, a = 0, b = 1, next;
    printf("How many terms? "); scanf("%d", &n);

    for (int i = 0; i < n; i++) {
        printf("%d ", a);
        next = a + b;
        a = b;
        b = next;
    }
    printf("\n");
    return 0;
}
terminal
output
How many terms? 8
0 1 1 2 3 5 8 13
program 4
4

Check If a Number Is Prime

A prime has no divisors other than 1 and itself — checking up to n/2 is enough.
prime_check.c
prime_check.c
C
#include <stdio.h>

int main() {
    int n, isPrime = 1;
    printf("Enter number: "); scanf("%d", &n);

    if (n < 2) isPrime = 0;
    else {
        for (int i = 2; i <= n / 2; i++)
            if (n % i == 0) { isPrime = 0; break; }
    }

    printf(isPrime ? "%d is PRIME\n" : "%d is NOT prime\n", n);
    return 0;
}
terminal
output
Enter number: 29
29 is PRIME
program 5
5

Print All Prime Numbers from 1 to n

Same prime-check logic, wrapped in an outer loop that tries every number up to n.
primes_upto_n.c
primes_upto_n.c
C
#include <stdio.h>

int main() {
    int n;
    printf("Print primes up to: "); scanf("%d", &n);

    for (int num = 2; num <= n; num++) {
        int isPrime = 1;
        for (int i = 2; i <= num / 2; i++)
            if (num % i == 0) { isPrime = 0; break; }
        if (isPrime) printf("%d ", num);
    }
    printf("\n");
    return 0;
}
terminal
output
Print primes up to: 30
2 3 5 7 11 13 17 19 23 29
program 6
6

GCD Using a Loop

The Greatest Common Divisor is the largest number that divides both — check every candidate downward from the smaller number.
gcd_loop.c
gcd_loop.c
C
#include <stdio.h>

int main() {
    int a, b, gcd = 1;
    printf("Enter two numbers: "); scanf("%d %d", &a, &b);

    for (int i = 1; i <= a && i <= b; i++) {
        if (a % i == 0 && b % i == 0)
            gcd = i;   // keep overwriting — the last match is the greatest
    }

    printf("GCD = %d\n", gcd);
    return 0;
}
terminal
output
Enter two numbers: 24 36
GCD = 12
program 7
7

LCM Using a Loop

The Least Common Multiple is the smallest number both divide into — count upward from the larger number until both fit.
lcm_loop.c
lcm_loop.c
C
#include <stdio.h>

int main() {
    int a, b, lcm;
    printf("Enter two numbers: "); scanf("%d %d", &a, &b);

    lcm = (a > b) ? a : b;   // start checking from the larger number
    while (1) {
        if (lcm % a == 0 && lcm % b == 0) break;
        lcm++;
    }

    printf("LCM = %d\n", lcm);
    return 0;
}
terminal
output
Enter two numbers: 4 6
LCM = 12
💡 Fun fact: GCD(a,b) × LCM(a,b) = a × b — always. For 24 and 36: GCD 12 × LCM 72 = 864 = 24 × 36. ✓
program 8 & 9
8-9

Print ASCII Values: A–Z and a–z

Characters are secretly small integers — looping through a char range prints letters and their ASCII codes together.
ascii_letters.c
ascii_letters.c
C
#include <stdio.h>

int main() {
    printf("Uppercase A-Z:\n");
    for (char c = 'A'; c <= 'Z'; c++)
        printf("%c = %d\n", c, c);

    printf("\nLowercase a-z:\n");
    for (char c = 'a'; c <= 'z'; c++)
        printf("%c = %d\n", c, c);

    return 0;
}
terminal — shortened
output
Uppercase A-Z:
A = 65
B = 66
...
Z = 90

Lowercase a-z:
a = 97
b = 98
...
z = 122
💡 Notice the gap: 'a' (97) is exactly 32 more than 'A' (65) — every lowercase letter is its uppercase version + 32, which is exactly how toupper()/tolower() work internally.
quiz
Q

Quick Quiz

Question 1 of 4

What makes 153 an Armstrong number?

Question 2 of 4

What's different between a Strong number and an Armstrong number?

Question 3 of 4

In the GCD loop, why does the code keep overwriting gcd instead of stopping at the first match?

Question 4 of 4

What is the relationship between 'A' and 'a' in ASCII?

Checklist

  • I can check Armstrong and Strong numbers
  • I can print the Fibonacci series
  • I can check primality and list primes up to n
  • I can compute GCD and LCM with loops
  • I can print ASCII values for A-Z and a-z
  • I completed the quiz