BACK-END

C Programs for Beginners: Fibonacci, Prime, Armstrong and Addition

Safikul Islam
By Safikul Islam Published Oct 27, 2025 · Updated Oct 20, 2026 · 5 min read · 0 comments
C Programs for Beginners: Fibonacci, Prime, Armstrong and Addition

These four C programs, Fibonacci series, prime number check, Armstrong number and addition of two numbers, cover loops, functions, recursion and input handling, which is most of what a beginner needs to practice. Each program below is complete, compiles with GCC, and I ran it on sample input to check the output.

To run any of them, save the code as a .c file, compile it with gcc file.c -o program, and run ./program.

1. Fibonacci series program in C

The Fibonacci series starts with 0 and 1, and each next number is the sum of the previous two: 0, 1, 1, 2, 3, 5, 8, 13. The rule is F(n) = F(n-1) + F(n-2).

Using a loop

Algorithm: set first = 0 and second = 1, then repeat n times: print first, compute next = first + second, and shift the values forward.

Flowchart of the iterative Fibonacci algorithm in C
#include <stdio.h>

int main(void) {
    int n;
    long long first = 0, second = 1, next;

    printf("Enter the number of terms: ");
    if (scanf("%d", &n) != 1 || n <= 0) {
        printf("Please enter a positive number.\n");
        return 1;
    }

    for (int i = 1; i <= n; i++) {
        printf("%lld ", first);
        next = first + second;
        first = second;
        second = next;
    }
    printf("\n");
    return 0;
}

Input 10 prints: 0 1 1 2 3 5 8 13 21 34

Using recursion

Recursion defines the function in terms of itself. It needs two base cases: n = 0 returns 0, and n = 1 returns 1.

Flowchart of the recursive Fibonacci algorithm in C
#include <stdio.h>

int fibonacci(int n) {
    if (n == 0) return 0;
    if (n == 1) return 1;
    return fibonacci(n - 1) + fibonacci(n - 2);
}

int main(void) {
    int n;
    printf("Enter the number of terms: ");
    if (scanf("%d", &n) != 1 || n <= 0) {
        printf("Please enter a positive number.\n");
        return 1;
    }
    for (int i = 0; i < n; i++) {
        printf("%d ", fibonacci(i));
    }
    printf("\n");
    return 0;
}

The recursive version recalculates the same values many times, so it gets slow for large n. Use the loop for anything beyond small inputs.

2. Prime number program in C

A prime number is greater than 1 and divisible only by 1 and itself. You only need to test divisors up to the square root of the number, so the loop condition is i * i <= n.

#include <stdio.h>

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

int main(void) {
    int n;
    printf("Enter a number: ");
    if (scanf("%d", &n) != 1) return 1;
    if (is_prime(n))
        printf("%d is a prime number.\n", n);
    else
        printf("%d is not a prime number.\n", n);
    return 0;
}

Input 29 prints “29 is a prime number.” and input 15 prints “15 is not a prime number.”

3. Armstrong number program in C

An Armstrong number equals the sum of its digits, each raised to the power of the number of digits. For example, 153 = 1³ + 5³ + 3³.

#include <stdio.h>

int main(void) {
    int n, original, digit, digits = 0, temp;
    long sum = 0;

    printf("Enter a number: ");
    if (scanf("%d", &n) != 1 || n < 0) return 1;

    original = n;
    temp = n;
    do {                    /* count digits, also handles 0 */
        digits++;
        temp /= 10;
    } while (temp > 0);

    temp = n;
    do {
        digit = temp % 10;
        long power = 1;
        for (int i = 0; i < digits; i++) power *= digit;
        sum += power;
        temp /= 10;
    } while (temp > 0);

    if (sum == original)
        printf("%d is an Armstrong number.\n", original);
    else
        printf("%d is not an Armstrong number.\n", original);
    return 0;
}

Input 153 prints “153 is an Armstrong number.” and input 123 prints “123 is not an Armstrong number.”

4. Addition of two numbers in C

This is the simplest program: read two numbers and print their sum. Using double lets it handle decimals, and checking the return value of scanf catches bad input.

#include <stdio.h>

int main(void) {
    double a, b;
    printf("Enter two numbers: ");
    if (scanf("%lf %lf", &a, &b) != 2) {
        printf("Invalid input.\n");
        return 1;
    }
    printf("Sum = %.2f\n", a + b);
    return 0;
}

Input 2.5 4 prints Sum = 6.50.

Common mistakes to avoid

  • Wrong base cases in recursion. Returning 1 for n = 0 gives wrong Fibonacci values.
  • Integer overflow. A 32-bit int overflows around the 47th Fibonacci number, so use long long for longer series.
  • Uninitialized variables. Set starting values before the loop.
  • Off-by-one loop conditions. i <= n versus i < n changes how many terms print.
  • Ignoring bad input. Always check the result of scanf.

Debugging tips

  • Print the variables at each loop step to see where the values go wrong.
  • Trace recursive calls on paper for a small input like n = 4.
  • Compile with gcc -Wall -Wextra to catch warnings early.

FAQs

What is the Fibonacci series program in C?

It prints a sequence where each number is the sum of the previous two, starting from 0 and 1, using a loop or recursion.

Is a loop or recursion better for Fibonacci?

A loop is faster and uses less memory. Recursion is useful for learning how base cases and function calls work.

Why does the prime check stop at the square root?

If a number has a factor larger than its square root, it must also have one smaller, so checking up to the square root is enough.

Tested with GCC on October 20, 2026. This guide combines my earlier separate posts on the prime number, Armstrong number and addition programs.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *