20+ Basic Practice Programs of C Programming

20+ Basic Practice Programs of C Programming

C is a versatile programming language used in a wide range of applications. Starting out by practicing with some fundamental programs is a smart approach when learning C.

You’ll find more than 20+ simple C programming practice programs in this blog article.

Practice Programs of C Programming

  1. A C program that prints “Hello, World!” to the screen.
#include <stdio.h>

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

  1. A C program that adds two numbers and prints the result.
#include <stdio.h>

int main() {
    int num1, num2, sum;
    
    printf("Enter first number: ");
    scanf("%d", &num1);
    
    printf("Enter second number: ");
    scanf("%d", &num2);
    
    sum = num1 + num2;
    printf("Sum: %d\n", sum);
    
    return 0;
}

  1. A C program that swaps the values of two numbers in between.
#include <stdio.h>

int main() {
    int num1, num2, temp;
    
    printf("Enter first number: ");
    scanf("%d", &num1);
    
    printf("Enter second number: ");
    scanf("%d", &num2);
    
    temp = num1;
    num1 = num2;
    num2 = temp;
    
    printf("After swapping:\n");
    printf("First number: %d\n", num1);
    printf("Second number: %d\n", num2);
    
    return 0;
}

  1. A C program that checks Whether a number is even or odd.
#include <stdio.h>

int main() {
    int num;
    
    printf("Enter a number: ");
    scanf("%d", &num);
    
    if (num % 2 == 0) {
        printf("%d is even.\n", num);
    } else {
        printf("%d is odd.\n", num);
    }
    
    return 0;
}

  1. A C program that checks whether a year is a leap year or not.
#include <stdio.h>

int main() {
    int year;
    
    printf("Enter a year: ");
    scanf("%d", &year);
    
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
        printf("%d is a leap year.\n", year);
    } else {
        printf("%d is not a leap year.\n", year);
    }
    
    return 0;
}

  1. A C program that calculates the factorial of a given number.
#include <stdio.h>

int main() {
    int num, factorial = 1, i;
    
    printf("Enter a number: ");
    scanf("%d", &num);
    
    for (i = 1; i <= num; i++) {
        factorial *= i;
    }
    
    printf("Factorial of %d: %d\n", num, factorial);
    
    return 0;
}

  1. A C program that checks whether a number is prime or not.
#include <stdio.h>

int main() {
    int num, i, flag = 0;
    
    printf("Enter a number: ");
    scanf("%d", &num);
    
    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;
}

  1. A C program that prints the Fibonacci series of up to a given number of terms.
#include <stdio.h>

int main() {
    int num, i, term1 = 0, term2 = 1, nextTerm;
    
    printf("Enter the number of terms: ");
    scanf("%d", &num);
    
    printf("Fibonacci Series: ");
    
    for (i = 1; i <= num; i++) {
        printf("%d, ", term1);
        nextTerm = term1 + term2;
        term1 = term2;
        term2 = nextTerm;
    }
    
    printf("\n");
    
    return 0;
}

  1. A C program that checks whether a number is a palindrome or not.
#include <stdio.h>

int main() {
    int num, reversedNum = 0, remainder, originalNum;
    
    printf("Enter a number: ");
    scanf("%d", &num);
    
    originalNum = num;
    
    while (num != 0) {
        remainder = num % 10;
        reversedNum = reversedNum * 10 + remainder;
        num /= 10;
    }
    
    if (originalNum == reversedNum) {
        printf("%d is a palindrome.\n", originalNum);
    } else {
        printf("%d is not a palindrome.\n", originalNum);
    }
    
    return 0;
}

  1. A C program that prints the ASCII value of any character.
#include <stdio.h>

int main() {
    char ch;
    
    printf("Enter a character: ");
    scanf("%c", &ch);
    
    printf("ASCII value of %c: %d\n", ch, ch);
    
    return 0;
}

  1. A C program that calculates the sum of the digits of a given number.
#include <stdio.h>

int main() {
    int num, sum = 0, remainder;
    
    printf("Enter a number: ");
    scanf("%d", &num);
    
    while (num != 0) {
        remainder = num % 10;
        sum += remainder;
        num /= 10;
    }
    
    printf("Sum of digits: %d\n", sum);
    
    return 0;
}

  1. A C program that reverses a given number.
#include <stdio.h>

int main() {
    int num, reversedNum = 0, remainder;
    
    printf("Enter a number: ");
    scanf("%d", &num);
    
    while (num != 0) {
        remainder = num % 10;
        reversedNum = reversedNum * 10 + remainder;
        num /= 10;
    }
    
    printf("Reversed number: %d\n", reversedNum);
    
    return 0;
}

  1. A C program that calculates the average of n numbers.
#include <stdio.h>

int main() {
    int n, i, num;
    float sum = 0.0, average;
    
    printf("Enter the total numbers: ");
    scanf("%d", &n);
    
    printf("Enter the numbers:\n");
    
    for (i = 1; i <= n; i++) {
        scanf("%d", &num);
        sum += num;
    }
    
    average = sum / n;
    
    printf("Average: %.2f\n",average);
    
    return 0;
}

  1. A C program that checks whether a number is an Armstrong number or not.
#include <stdio.h>

int main() {
    int num, originalNum, remainder, result = 0;
    
    printf("Enter a three-digit number: ");
    scanf("%d", &num);
    
    originalNum = num;
    
    while (originalNum != 0) {
        remainder = originalNum % 10;
        result += remainder * remainder * remainder;
        originalNum /= 10;
    }
    
    if (result == num) {
        printf("%d is an Armstrong number.\n", num);
    } else {
        printf("%d is not an Armstrong number.\n", num);
    }
    
    return 0;
}

  1. A C program that prints the multiplication table of a given number.
#include <stdio.h>

int main() {
    int num, i;
    
    printf("Enter a number: ");
    scanf("%d", &num);
    
    printf("Multiplication Table of %d:\n", num);
    
    for (i = 1; i <= 10; i++) {
        printf("%d x %d = %d\n", num, i, num * i);
    }
    
    return 0;
}

  1. A C program that calculates the power of a number.
#include <stdio.h>

int main() {
    int base, exponent;
    long long result = 1;
    
    printf("Enter base: ");
    scanf("%d", &base);
    
    printf("Enter exponent: ");
    scanf("%d", &exponent);
    
    while (exponent != 0) {
        result *= base;
        --exponent;
    }
    
    printf("Result: %lld\n", result);
    
    return 0;
}

  1. A C program that checks whether a character is a vowel or consonant.
#include <stdio.h>

int main() {
    char ch;
    
    printf("Enter a character: ");
    scanf(" %c", &ch);
    
    if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
        ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
        printf("%c is a vowel.\n", ch);
    } else {
        printf("%c is a consonant.\n", ch);
    }
    
    return 0;
}

  1. A C program that calculates the greatest common divisor (GCD) of two given numbers.
#include <stdio.h>

int main() {
    int num1, num2, i, gcd;
    
    printf("Enter first number: ");
    scanf("%d", &num1);
    
    printf("Enter second number: ");
    scanf("%d", &num2);
    
    for (i = 1; i <= num1 && i <= num2; ++i) {
        if (num1 % i == 0 && num2 % i == 0) {
            gcd = i;
        }
    }
    
    printf("GCD of %d and %d: %d\n", num1, num2, gcd);
    
    return 0;
}

  1. A C program that calculates the least common multiple (LCM) of two given numbers.
#include <stdio.h>

int main() {
    int num1, num2, max, lcm;
    
    printf("Enter first number: ");
    scanf("%d", &num1);
    
    printf("Enter second number: ");
    scanf("%d", &num2);
    
    max = (num1 > num2) ? num1 : num2;
    
    while (1) {
        if (max % num1 == 0 && max % num2 == 0) {
            lcm = max;
            break;
        }
        ++max;
    }
    
    printf("LCM of %d and %d: %d\n", num1, num2, lcm);
    
    return 0;
}

  1. A C program that calculates the simple interest of an amount.
#include <stdio.h>

int main() {
    float principal, rate, time, simpleInterest;
    
    printf("Enter principal amount: ");
    scanf("%f", &principal);
    
    printf("Enter rate of interest: ");
    scanf("%f", &rate);
    
    printf("Enter time period (in years): ");
    scanf("%f", &time);
    
    simpleInterest = (principal * rate * time) / 100;
    
    printf("Simple Interest: %.2f\n", simpleInterest);
    
    return 0;
}

These are 20 basic C programs that cover a range of concepts in basic C programming. Feel free to explore and modify them as per your requirements.

About The Author

Leave a reply

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