C Programming Practice Programs for Beginners – Fundamentals, Conditions & Loops

C Programming Practice Programs for Beginners – Fundamentals, Conditions & Loops

1. Program to Print Your Complete Name
#include <stdio.h>

int main() {
    printf("My Name is John Doe\n");
    return 0;
}
    
Output:
My Name is John Doe
2. Perform All Arithmetic Operations
#include <stdio.h>

int main() {
    int a, b;
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);

    printf("Addition = %d\n", a + b);
    printf("Subtraction = %d\n", a - b);
    printf("Multiplication = %d\n", a * b);
    printf("Division = %d\n", a / b);
    printf("Modulus = %d\n", a % b);

    return 0;
}
    
Enter two numbers: 10 5 Addition = 15 Subtraction = 5 Multiplication = 50 Division = 2 Modulus = 0
3. Calculate Simple Interest
#include <stdio.h>

int main() {
    float p, r, t, si;
    printf("Enter Principal, Rate and Time: ");
    scanf("%f %f %f", &p, &r, &t);

    si = (p * r * t) / 100;
    printf("Simple Interest = %.2f\n", si);
    return 0;
}
    
Enter Principal, Rate and Time: 1000 5 2 Simple Interest = 100.00
4. Area of Circle
#include <stdio.h>

int main() {
    float radius, area;
    printf("Enter radius of circle: ");
    scanf("%f", &radius);

    area = 3.14 * radius * radius;
    printf("Area of Circle = %.2f\n", area);
    return 0;
}
    
Enter radius of circle: 7 Area of Circle = 153.86
5. Area of Rectangle
#include <stdio.h>

int main() {
    float length, width, area;
    printf("Enter length and width: ");
    scanf("%f %f", &length, &width);

    area = length * width;
    printf("Area of Rectangle = %.2f\n", area);
    return 0;
}
    
Enter length and width: 5 10 Area of Rectangle = 50.00
6. Total Marks and Percentage
#include <stdio.h>

int main() {
    int m1, m2, m3, m4, m5, total;
    float percent;

    printf("Enter marks of 5 subjects (out of 100): ");
    scanf("%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5);

    total = m1 + m2 + m3 + m4 + m5;
    percent = (float)total / 5;

    printf("Total Marks = %d\n", total);
    printf("Percentage = %.2f%%\n", percent);
    return 0;
}
    
Enter marks of 5 subjects (out of 100): 80 75 90 85 70 Total Marks = 400 Percentage = 80.00%
7. Square and Cube of a Number
#include <stdio.h>

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);

    printf("Square = %d\n", num * num);
    printf("Cube = %d\n", num * num * num);
    return 0;
}
    
Enter a number: 3 Square = 9 Cube = 27
8. Swap Two Numbers (Using 3rd Variable)
#include <stdio.h>

int main() {
    int a, b, temp;
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);

    temp = a;
    a = b;
    b = temp;

    printf("After swapping: a = %d, b = %d\n", a, b);
    return 0;
}
    
Enter two numbers: 5 10 After swapping: a = 10, b = 5
9. Swap Without 3rd Variable
#include <stdio.h>

int main() {
    int a, b;
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);

    a = a + b;
    b = a - b;
    a = a - b;

    printf("After swapping: a = %d, b = %d\n", a, b);
    return 0;
}
    
Enter two numbers: 7 3 After swapping: a = 3, b = 7
10. Fahrenheit to Celsius
#include <stdio.h>

int main() {
    float fahrenheit, celsius;
    printf("Enter temperature in Fahrenheit: ");
    scanf("%f", &fahrenheit);

    celsius = (fahrenheit - 32) * 5 / 9;
    printf("Temperature in Celsius = %.2f°C\n", celsius);
    return 0;
}
    
Enter temperature in Fahrenheit: 98.6 Temperature in Celsius = 37.00°C
1. Find Maximum of Two Numbers
#include <stdio.h>

int main() {
    int a, b;
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);

    if(a > b)
        printf("Maximum = %d\n", a);
    else
        printf("Maximum = %d\n", b);

    return 0;
}
Enter two numbers: 10 20 Maximum = 20
2. Voting Eligibility
#include <stdio.h>

int main() {
    int age;
    printf("Enter your age: ");
    scanf("%d", &age);

    if(age >= 18)
        printf("Person is eligible to vote.\n");
    else
        printf("Person is not eligible to vote.\n");

    return 0;
}
Enter your age: 17 Person is not eligible to vote.
3. Check 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;
}
Enter a number: 4 4 is Even
4. Check Positive or Negative
#include <stdio.h>

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);

    if(num > 0)
        printf("Positive Number\n");
    else if(num < 0)
        printf("Negative Number\n");
    else
        printf("Zero\n");

    return 0;
}
Enter a number: -5 Negative Number
5. Discount Based on Total Purchase
#include <stdio.h>

int main() {
    float price, qty, total, discount, payment;
    printf("Enter price and quantity: ");
    scanf("%f %f", &price, &qty);

    total = price * qty;
    if(total > 1000)
        discount = total * 0.15;
    else
        discount = total * 0.10;

    payment = total - discount;
    printf("Final Payment = %.2f\n", payment);

    return 0;
}
Enter price and quantity: 200 6 Final Payment = 1020.00
6. Check Leap Year
#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;
}
Enter a year: 2024 2024 is a Leap Year
7. Check Divisibility
#include <stdio.h>

int main() {
    int a, b;
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);

    if (b != 0 && a % b == 0)
        printf("%d is divisible by %d\n", a, b);
    else
        printf("%d is not divisible by %d\n", a, b);

    return 0;
}
Enter two numbers: 10 5 10 is divisible by 5
8. Print 1 2 3 4 ... N
#include <stdio.h>

int main() {
    int i, n;
    printf("Enter N: ");
    scanf("%d", &n);

    for(i = 1; i <= n; i++) {
        printf("%d ", i);
    }
    return 0;
}
Enter N: 5 1 2 3 4 5
9. Print 1 8 27 64 ... N Cubes
#include <stdio.h>

int main() {
    int i, n;
    printf("Enter N: ");
    scanf("%d", &n);

    for(i = 1; i <= n; i++) {
        printf("%d ", i*i*i);
    }
    return 0;
}
Enter N: 4 1 8 27 64
10. Print 1 3 5 7 ... N (Odd Numbers)
#include <stdio.h>

int main() {
    int i, n;
    printf("Enter N (limit): ");
    scanf("%d", &n);

    for(i = 1; i <= n; i += 2) {
        printf("%d ", i);
    }
    return 0;
}
Enter N: 9 1 3 5 7 9
11. Left-Aligned Triangle using Special Symbol
#include <stdio.h>

int main() {
    char symbol;
    int i, j;
    printf("Enter a symbol: ");
    scanf(" %c", &symbol);
    for(i = 1; i <= 5; i++) {
        for(j = 1; j <= i; j++) {
            printf("%c ", symbol);
        }
        printf("\n");
    }
    return 0;
}
Enter a symbol: * * * * * * * * * * * * * * * *
12. Right-Aligned Triangle of Numbers
#include <stdio.h>

int main() {
    int i, j, space;
    for(i = 1; i <= 5; i++) {
        for(space = 5; space > i; space--)
            printf("  ");
        for(j = 1; j <= i; j++)
            printf("%d ", j);
        printf("\n");
    }
    return 0;
}
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
13. Diamond Shape of Given Symbol
#include <stdio.h>

int main() {
    char symbol;
    int i, j, space;
    printf("Enter symbol: ");
    scanf(" %c", &symbol);

    for(i = 1; i <= 5; i++) {
        for(space = 5 - i; space > 0; space--)
            printf(" ");
        for(j = 1; j <= (2*i - 1); j++)
            printf("%c", symbol);
        printf("\n");
    }
    for(i = 4; i >= 1; i--) {
        for(space = 5 - i; space > 0; space--)
            printf(" ");
        for(j = 1; j <= (2*i - 1); j++)
            printf("%c", symbol);
        printf("\n");
    }
    return 0;
}
Enter symbol: * * *** ***** ******* ********* ******* ***** *** *
14. Multiplication Table of a Number
#include <stdio.h>

int main() {
    int n, i;
    printf("Enter a number: ");
    scanf("%d", &n);
    for(i = 1; i <= 10; i++) {
        printf("%d x %d = %d\n", n, i, n * i);
    }
    return 0;
}
Enter a number: 5 5 x 1 = 5 ... 5 x 10 = 50
15. Fibonacci Series up to n Terms
#include <stdio.h>

int main() {
    int n, a = 0, b = 1, c, i;
    printf("Enter n: ");
    scanf("%d", &n);
    printf("%d %d ", a, b);
    for(i = 3; i <= n; i++) {
        c = a + b;
        printf("%d ", c);
        a = b;
        b = c;
    }
    return 0;
}
Enter n: 7 0 1 1 2 3 5 8
16. Sum of Digits
#include <stdio.h>

int main() {
    int num, sum = 0, digit;
    printf("Enter a number: ");
    scanf("%d", &num);
    while(num != 0) {
        digit = num % 10;
        sum += digit;
        num /= 10;
    }
    printf("Sum of digits = %d\n", sum);
    return 0;
}
Enter a number: 123 Sum of digits = 6
17. Reverse of a Number
#include <stdio.h>

int main() {
    int num, rev = 0;
    printf("Enter a number: ");
    scanf("%d", &num);
    while(num != 0) {
        rev = rev * 10 + num % 10;
        num /= 10;
    }
    printf("Reversed number = %d\n", rev);
    return 0;
}
Enter a number: 123 Reversed number = 321
18. Check Palindrome Number
#include <stdio.h>

int main() {
    int num, original, rev = 0;
    printf("Enter a number: ");
    scanf("%d", &num);
    original = num;
    while(num != 0) {
        rev = rev * 10 + num % 10;
        num /= 10;
    }
    if(original == rev)
        printf("Palindrome number\n");
    else
        printf("Not a palindrome\n");
    return 0;
}
Enter a number: 121 Palindrome number
19. Check Armstrong Number
#include <stdio.h>
#include <math.h>

int main() {
    int num, original, rem, result = 0, n = 0;
    printf("Enter a number: ");
    scanf("%d", &num);
    original = num;
    int temp = num;
    while(temp != 0) {
        temp /= 10;
        n++;
    }
    temp = num;
    while(temp != 0) {
        rem = temp % 10;
        result += pow(rem, n);
        temp /= 10;
    }
    if(result == original)
        printf("Armstrong number\n");
    else
        printf("Not an Armstrong number\n");
    return 0;
}
Enter a number: 153 Armstrong number
20. Check Prime Number
#include <stdio.h>

int main() {
    int num, i, flag = 0;
    printf("Enter a number: ");
    scanf("%d", &num);
    if(num <= 1) flag = 1;
    for(i = 2; i <= num/2; i++) {
        if(num % i == 0) {
            flag = 1;
            break;
        }
    }
    if(flag == 0)
        printf("Prime number\n");
    else
        printf("Not a prime number\n");
    return 0;
}
Enter a number: 7 Prime number
21. Prime Numbers up to N
#include <stdio.h>

int main() {
    int n, i, j, flag;
    printf("Enter the limit: ");
    scanf("%d", &n);
    printf("Prime numbers up to %d:\n", n);
    for(i = 2; i <= n; i++) {
        flag = 0;
        for(j = 2; j <= i/2; j++) {
            if(i % j == 0) {
                flag = 1;
                break;
            }
        }
        if(flag == 0)
            printf("%d ", i);
    }
    return 0;
}
Enter the limit: 10 Prime numbers up to 10: 2 3 5 7
22. Armstrong Numbers up to N
#include <stdio.h>
#include <math.h>

int main() {
    int n, num, rem, temp, result, digits;
    printf("Enter the limit: ");
    scanf("%d", &n);
    printf("Armstrong numbers up to %d:\n", n);
    for(num = 1; num <= n; num++) {
        temp = num;
        digits = 0;
        result = 0;

        while(temp != 0) {
            temp /= 10;
            digits++;
        }

        temp = num;
        while(temp != 0) {
            rem = temp % 10;
            result += pow(rem, digits);
            temp /= 10;
        }

        if(result == num)
            printf("%d ", num);
    }
    return 0;
}
Enter the limit: 500 Armstrong numbers up to 500: 1 2 3 4 5 6 7 8 9 153 370 371 407
23. Factorial of a Number
#include <stdio.h>

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

Post a Comment

0 Comments