Basic C Programs

Essential C programs for beginners. Learn fundamental concepts with these simple and practical examples.

Hello World Program

Beginner

The classic first program that prints "Hello, World!" to the console.

#include <stdio.h>

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

Simple Calculator

Beginner

A basic calculator that performs arithmetic operations based on user input.

#include <stdio.h>

int main() {
    float num1, num2, result;
    char operator;
    
    printf("Enter first number: ");
    scanf("%f", &num1);
    
    printf("Enter operator (+, -, *, /): ");
    scanf(" %c", &operator);
    
    printf("Enter second number: ");
    scanf("%f", &num2);
    
    switch(operator) {
        case '+':
            result = num1 + num2;
            break;
        case '-':
            result = num1 - num2;
            break;
        case '*':
            result = num1 * num2;
            break;
        case '/':
            if(num2 != 0) {
                result = num1 / num2;
            } else {
                printf("Error: Division by zero!\n");
                return 1;
            }
            break;
        default:
            printf("Invalid operator!\n");
            return 1;
    }
    
    printf("Result: %.2f\n", result);
    return 0;
}
Try This Code

Prime Number Checker

Beginner

Checks whether a given number is prime or not.

#include <stdio.h>

int main() {
    int num, i, flag = 0;
    
    printf("Enter a positive integer: ");
    scanf("%d", &num);
    
    if (num <= 1) {
        printf("%d is not a prime number.\n", num);
        return 0;
    }
    
    for (i = 2; i <= num / 2; i++) {
        if (num % i == 0) {
            flag = 1;
            break;
        }
    }
    
    if (flag == 0) {
        printf("%d is a prime number.\n", num);
    } else {
        printf("%d is not a prime number.\n", num);
    }
    
    return 0;
}
Try This Code

Factorial Calculator

Beginner

Calculates the factorial of a given number using a loop.

#include <stdio.h>

int main() {
    int num, i;
    long long factorial = 1;
    
    printf("Enter a positive integer: ");
    scanf("%d", &num);
    
    if (num < 0) {
        printf("Error: Factorial of negative number doesn't exist.\n");
        return 1;
    }
    
    for (i = 1; i <= num; i++) {
        factorial *= i;
    }
    
    printf("Factorial of %d = %lld\n", num, factorial);
    return 0;
}
Try This Code

Fibonacci Series

Beginner

Generates the Fibonacci series up to n terms.

#include <stdio.h>

int main() {
    int n, i, first = 0, second = 1, next;
    
    printf("Enter number of terms: ");
    scanf("%d", &n);
    
    if (n <= 0) {
        printf("Please enter a positive integer.\n");
        return 1;
    }
    
    printf("Fibonacci Series: ");
    for (i = 0; i < n; i++) {
        if (i <= 1) {
            next = i;
        } else {
            next = first + second;
            first = second;
            second = next;
        }
        printf("%d ", next);
    }
    printf("\n");
    
    return 0;
}
Try This Code

Number Reversal

Beginner

Reverses the digits of a given number.

#include <stdio.h>

int main() {
    int num, reversed = 0, remainder;
    
    printf("Enter an integer: ");
    scanf("%d", &num);
    
    while (num != 0) {
        remainder = num % 10;
        reversed = reversed * 10 + remainder;
        num /= 10;
    }
    
    printf("Reversed number: %d\n", reversed);
    return 0;
}
Try This Code

Temperature Converter

Beginner

Converts temperatures between Celsius and Fahrenheit scales.

#include <stdio.h>

int main() {
    float celsius, fahrenheit;
    int choice;
    
    printf("Temperature Converter\n");
    printf("1. Celsius to Fahrenheit\n");
    printf("2. Fahrenheit to Celsius\n");
    printf("Enter your choice (1 or 2): ");
    scanf("%d", &choice);
    
    if (choice == 1) {
        printf("Enter temperature in Celsius: ");
        scanf("%f", &celsius);
        fahrenheit = (celsius * 9.0 / 5.0) + 32;
        printf("%.2f°C = %.2f°F\n", celsius, fahrenheit);
    } else if (choice == 2) {
        printf("Enter temperature in Fahrenheit: ");
        scanf("%f", &fahrenheit);
        celsius = (fahrenheit - 32) * 5.0 / 9.0;
        printf("%.2f°F = %.2f°C\n", fahrenheit, celsius);
    } else {
        printf("Invalid choice!\n");
    }
    
    return 0;
}
Try This Code

Simple Interest Calculator

Beginner

Calculates simple interest and total amount.

#include <stdio.h>

int main() {
    float principal, rate, time, simple_interest;
    
    printf("Enter principal amount: ");
    scanf("%f", &principal);
    
    printf("Enter rate of interest (per annum): ");
    scanf("%f", &rate);
    
    printf("Enter time period (in years): ");
    scanf("%f", &time);
    
    simple_interest = (principal * rate * time) / 100;
    
    printf("\nSimple Interest = %.2f\n", simple_interest);
    printf("Total Amount = %.2f\n", principal + simple_interest);
    
    return 0;
}
Try This Code

Ready for More Advanced Programs?

Once you're comfortable with these basic programs, explore our more advanced examples.