std::optional

What Are 10 Examples of C Programs?

Ten short, complete C programs with their output: Hello World, adding two numbers, even or odd, factorial, Fibonacci, prime check and palindrome.

On this page
  1. Compiling the examples
  2. The ten programs
  3. C and C++

Here are ten short C programs, from a Hello, World program to a palindrome check. Each one is complete, so you can save it to a file, compile it and run it. The output follows each program.

Compiling the examples

Save a program as a .c file, then compile and run it. For example:

gcc -std=c99 -Wall hello.c -o hello
./hello

The programs need C99 or later, because they declare loop variables inside for statements. The -Wall flag makes the compiler report common problems.

The ten programs

1. Hello, World

The traditional first program prints a single line of text.

#include <stdio.h>

int main(void) {
    printf("Hello, World!\n");
    return 0;
}

Output:

Hello, World!

2. Adding two numbers

This program adds two integers and prints the result.

#include <stdio.h>

int main(void) {
    int a = 7;
    int b = 5;
    int sum = a + b;
    printf("%d + %d = %d\n", a, b, sum);
    return 0;
}

Output:

7 + 5 = 12

3. Even or odd

A number is even when dividing it by 2 leaves no remainder. The % operator gives that remainder.

#include <stdio.h>

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

Output:

13 is odd

4. Multiplication table

A for loop prints the first five multiples of 3.

#include <stdio.h>

int main(void) {
    int n = 3;
    for (int i = 1; i <= 5; i++) {
        printf("%d x %d = %d\n", n, i, n * i);
    }
    return 0;
}

Output:

3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15

5. Factorial

This program multiplies the numbers from 1 to 5, giving 120. It uses long long so that larger factorials have room. A loop works here, and the same result can come from a recursive function in c, where the function calls itself with a smaller number.

#include <stdio.h>

int main(void) {
    int n = 5;
    long long result = 1;
    for (int i = 2; i <= n; i++) {
        result *= i;
    }
    printf("%d! = %lld\n", n, result);
    return 0;
}

Output:

5! = 120

6. Fibonacci sequence

Each number in the Fibonacci sequence is the sum of the two before it. This program prints the first ten numbers.

#include <stdio.h>

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

Output:

0 1 1 2 3 5 8 13 21 34

7. Largest value in an array

sizeof gives the size of the whole array in bytes. Dividing by the size of one element gives the number of elements. The loop keeps the biggest value seen so far.

#include <stdio.h>

int main(void) {
    int values[] = {4, 19, 7, 23, 11};
    int count = sizeof(values) / sizeof(values[0]);
    int largest = values[0];
    for (int i = 1; i < count; i++) {
        if (values[i] > largest) {
            largest = values[i];
        }
    }
    printf("Largest: %d\n", largest);
    return 0;
}

Output:

Largest: 23

8. Prime number check

A prime number has no divisors other than 1 and itself. The loop only tests divisors up to the square root of the number, which is enough to find any factor.

#include <stdio.h>

int main(void) {
    int n = 29;
    int is_prime = 1;

    if (n < 2) {
        is_prime = 0;
    }
    for (int d = 2; d * d <= n; d++) {
        if (n % d == 0) {
            is_prime = 0;
            break;
        }
    }
    printf("%d is %s\n", n, is_prime ? "prime" : "not prime");
    return 0;
}

Output:

29 is prime

9. Celsius to Fahrenheit

The formula is F = C × 9/5 + 32. The program uses double for decimal numbers and prints one digit after the decimal point.

#include <stdio.h>

int main(void) {
    double celsius = 25.0;
    double fahrenheit = celsius * 9.0 / 5.0 + 32.0;
    printf("%.1f C is %.1f F\n", celsius, fahrenheit);
    return 0;
}

Output:

25.0 C is 77.0 F

10. Palindrome check

A palindrome reads the same forwards and backwards. The program compares characters from both ends and moves inward. The string must not be empty.

#include <stdio.h>
#include <string.h>

int main(void) {
    char word[] = "level";
    size_t left = 0;
    size_t right = strlen(word) - 1;
    int is_palindrome = 1;

    while (left < right) {
        if (word[left] != word[right]) {
            is_palindrome = 0;
            break;
        }
        left++;
        right--;
    }
    printf("%s is %s\n", word, is_palindrome ? "a palindrome" : "not a palindrome");
    return 0;
}

Output:

level is a palindrome

C and C++

C and C++ are related languages, but they are separate. C++ adds features such as classes, templates and std::cout, which these examples don’t use. Each program sticks to features that both languages share, so each one also builds with a C++ compiler.

The complete guide to std::optional

std::optional in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

How to Use goto in C++

Learn how to use goto in C++: write a label and jump to it, see forward and backward examples, avoid compile errors, and pick safer alternatives.

goto 5 min read

The inline Keyword in C++

Learn how the inline keyword in C++ works, why inline functions belong in header files, how inline variables work in C++17, and when the compiler ignores it.

inline 3 min read