CodeMIRAI
0 XP

Classic number programs

2 min read 5 runnable examples Quiz included

These programs come up again and again in classes, tests and viva questions. Each is a small function built from loops and conditions. Run each one, then change the numbers.

Even or odd, and leap years

A leap year divides by 4, except century years, which must also divide by 400:

C
#include <stdio.h>

int isLeap(int year) {
    return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}

int main() {
    int n = 7;
    if (n % 2 == 0) printf("%d is even\n", n);
    else printf("%d is odd\n", n);

    int years[4] = {2024, 2026, 1900, 2000};
    for (int i = 0; i < 4; i++) {
        if (isLeap(years[i])) printf("%d is a leap year\n", years[i]);
        else printf("%d is not a leap year\n", years[i]);
    }
    return 0;
}

C has no true/false without extra headers: a condition is just a number, 0 for false and anything else for true.

Prime numbers

A prime has exactly two divisors, 1 and itself. Test divisors only while d * d <= n (up to the square root): a bigger factor would pair with a smaller one already tested.

C
#include <stdio.h>

int isPrime(int n) {
    if (n < 2) return 0;
    for (int d = 2; d * d <= n; d++) {
        if (n % d == 0) return 0;
    }
    return 1;
}

int main() {
    int count = 0;
    for (int n = 2; n <= 50; n++) {
        if (isPrime(n)) {
            printf("%d ", n);
            count++;
        }
    }
    printf("\n%d primes up to 50\n", count);
    return 0;
}

Fibonacci and swapping

Each Fibonacci number is the sum of the two before it. Moving the pair along uses the same idea as swapping two variables with a temporary:

C
#include <stdio.h>

int main() {
    int a = 0, b = 1;
    for (int i = 1; i <= 10; i++) {
        printf("%d ", a);
        int next = a + b;
        a = b;
        b = next;
    }
    printf("\n");

    int x = 5, y = 9;
    int temp = x;         /* the classic swap */
    x = y;
    y = temp;
    printf("after the swap: x = %d, y = %d\n", x, y);
    return 0;
}

Palindromes and Armstrong numbers

A palindrome number reads the same reversed (121). An Armstrong number equals the sum of its digits, each raised to the number of digits (153 = 1 + 125 + 27):

C
#include <stdio.h>

int reverse(int n) {
    int r = 0;
    while (n > 0) {
        r = r * 10 + n % 10;
        n = n / 10;
    }
    return r;
}

int isArmstrong(int n) {
    int digits = 0, rest = n, sum = 0;
    while (rest > 0) { digits++; rest = rest / 10; }
    rest = n;
    while (rest > 0) {
        int d = rest % 10, power = 1;
        for (int k = 0; k < digits; k++) power = power * d;
        sum += power;
        rest = rest / 10;
    }
    return sum == n;
}

int main() {
    int tests[3] = {121, 12321, 123};
    for (int i = 0; i < 3; i++) {
        printf("%d palindrome: %d\n", tests[i], reverse(tests[i]) == tests[i]);
    }
    printf("Armstrong numbers up to 1000: ");
    for (int n = 1; n <= 1000; n++) {
        if (isArmstrong(n)) printf("%d ", n);
    }
    printf("\n");
    return 0;
}

GCD and LCM

Euclid's method replaces (a, b) with (b, a % b) until the remainder is 0. Then LCM = a x b / GCD:

C
#include <stdio.h>

int gcd(int a, int b) {
    while (b != 0) {
        int r = a % b;
        a = b;
        b = r;
    }
    return a;
}

int main() {
    int x = 48, y = 36;
    printf("GCD of %d and %d is %d\n", x, y, gcd(x, y));
    printf("LCM of %d and %d is %d\n", x, y, x * y / gcd(x, y));
    return 0;
}
Finished this lesson?Mark it complete to earn +20 XP and move your progress on.