Easy C Code Snippets

Explore these C code examples that you can easily copy and use in your own projects.

Basic Hello World

// Include stdio.h
#include 

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

Simple For Loop

// Include stdio.h
#include 

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

Basic Calculator

// Include stdio.h
#include 

int main() {
    char operator;
    double first, second;
    printf("Enter an operator (+, -, *, /): ");
    scanf("%c", &operator);
    printf("Enter two operands: ");
    scanf("%lf %lf", &first, &second);

    switch (operator) {
        case '+':
            printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
            break;
        case '-':
            printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
            break;
        case '*':
            printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
            break;
        case '/':
            printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
            break;
        default:
            printf("Operator is not correct");
    }

    return 0;
}                      
                    

Temperature Converter

// Include stdio.h
#include 

double celsiusToFahrenheit(double celsius) {
    return (celsius * 9 / 5) + 32;
}

double fahrenheitToCelsius(double fahrenheit) {
    return (fahrenheit - 32) * 5 / 9;
}

int main() {
    double temperature;
    char unit;
    printf("Enter temperature with unit (C or F): ");
    scanf("%lf %c", &temperature, &unit);

    if (unit == 'C' || unit == 'c') {
        printf("%.2lf C = %.2lf F", temperature, celsiusToFahrenheit(temperature));
    } else if (unit == 'F' || unit == 'f') {
        printf("%.2lf F = %.2lf C", temperature, fahrenheitToCelsius(temperature));
    } else {
        printf("Invalid unit");
    }

    return 0;
}                       
                    

Guess the Number Game

// Include stdio.h, stdlib.h and time.h
#include 

int main() {
int number, guess, attempts = 0;
srand(time(0));
number = rand() % 100 + 1; // Random number between 1 and 100

printf("Guess the number (1 to 100): ");

do {
    scanf("%d", &guess);
    attempts++;

    if (guess > number) {
        printf("Lower!\n");
    } else if (guess < number) {
        printf("Higher!\n");
    } else {
        printf("You guessed it in %d attempts!", attempts);
    }
} while (guess != number);

return 0;
}             
                

Simple ATM Interface

// Include stdio.h
#include

int main() {
    int choice;
    float balance = 1000.00;
    float amount;

    while (1) {
        printf("ATM Menu:\n");
        printf("1. Check Balance\n");
        printf("2. Withdraw Money\n");
        printf("3. Deposit Money\n");
        printf("4. Exit\n");
        printf("Choose an option: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                printf("Balance: $%.2f\n", balance);
                break;
            case 2:
                printf("Enter amount to withdraw: ");
                scanf("%f", &amount);
                if (amount > balance) {
                    printf("Insufficient balance\n");
                } else {
                    balance -= amount;
                    printf("New balance: $%.2f\n", balance);
                }
                break;
            case 3:
                printf("Enter amount to deposit: ");
                scanf("%f", &amount);
                balance += amount;
                printf("New balance: $%.2f\n", balance);
                break;
            case 4:
                printf("Exiting...\n");
                return 0;
            default:
                printf("Invalid option\n");
        }
    }

    return 0;
}                      
                    

Palindrome Checker

// Include stdio.h, string.h and stdbool.h
#include

bool isPalindrome(char str[]) {
    int l = 0;
    int h = strlen(str) - 1;

    while (h > l) {
        if (str[l++] != str[h--]) {
            return false;
        }
    }
    return true;
}

int main() {
    char str[100];
    printf("Enter a string: ");
    scanf("%s", str);

    if (isPalindrome(str))
        printf("%s is a palindrome.", str);
    else
        printf("%s is not a palindrome.", str);

    return 0;
}                   
                    

Simple Interest Calculator

// Include stdio.h
#include

int main() {
    double principal, rate, time, interest;

    printf("Enter principal amount: ");
    scanf("%lf", &principal);

    printf("Enter rate of interest: ");
    scanf("%lf", &rate);

    printf("Enter time period (in years): ");
    scanf("%lf", &time);

    interest = (principal * rate * time) / 100.0;

    printf("Simple Interest = %.2lf\n", interest);

    return 0;
}                       
                    

Factorial Calculator

// Include stdio.h
#include

unsigned long long factorial(int n) {
    if (n == 0 || n == 1) {
        return 1;
    } else {
        return n * factorial(n - 1);
    }
}

int main() {
    int num;

    printf("Enter a positive integer: ");
    scanf("%d", &num);

    if (num < 0) {
        printf("Factorial is not defined for negative numbers.\n");
    } else {
        unsigned long long fact = factorial(num);
        printf("Factorial of %d = %llu\n", num, fact);
    }

    return 0;
}
                        
                    

Fibonacci Series Generator

// Include stdio.h
#include

void generateFibonacci(int n) {
    int a = 0, b = 1, next;

    printf("Fibonacci Series (first %d terms): ", n);

    for (int i = 1; i <= n; i++) {
        if (i == 1)
            printf("%d, ", a);
        else if (i == 2)
            printf("%d, ", b);
        else {
            next = a + b;
            printf("%d, ", next);
            a = b;
            b = next;
        }
    }
    printf("\n");
}

int main() {
    int terms;

    printf("Enter the number of terms for the Fibonacci series: ");
    scanf("%d", &terms);

    generateFibonacci(terms);

    return 0;
}                      
                    

Prime Number Checker

// Include stdio.h and stdbool.h
#include
                        
bool isPrime(int num) {
    if (num <= 1)
        return false;

    for (int i = 2; i * i <= num; i++) {
        if (num % i == 0)
            return false;
    }

    return true;
}

int main() {
    int num;

    printf("Enter a positive integer: ");
    scanf("%d", &num);

    if (isPrime(num)) {
        printf("%d is a prime number.\n", num);
    } else {
        printf("%d is not a prime number.\n", num);
    }

    return 0;
}