Some Basic C programs to practice-Part One

Suyog Acharya
15 Min Read
Contents: Click for quick access
1. Write a C program to print the message “Welcome to C programming”2. Write a program to print first 13 odd numbers3. Write a C program to input a number and print the square of a number.4. Write a C program to input a number and print the cube of a number.5. Write a C program to input a number and print the square and cube of a number.6. Write a C program to input two numbers and find the sum of the numbers.7. Write a C program to input two numbers and find the product of the numbers.8. Write a C program to input two numbers and find the sum, product and difference of the numbers.9. Write a program in C to input a length and find the area and perimeter of square.10. Write a program in C to input length and breadth and find the area and perimeter of rectangle. 11. Write a program in C to input length, breadth and height and find the volume of box.12. Write a program in C to input base and height and find the area of triangle.13. Write a program in C to input principle, rate and time and calculate simple interest.14. Write a program in C to input radius and find the area of circle (with #define also).15. Write a program in C to input radius and find the circumference of circle (with #define ).16. Write a program in C to input the diameter and find the area and circumference of the circle. (with #define also)17. Write a program in C to input radius and height and find the volume of cylinder. (with #define also)18. Using sqrt function find the square root of an input number.19. Using pow function find the square and cube of an input number.20. Write a program to ask if a number is a perfect number or not.21. Using sqrt and pow function find the square root and cube root of a number.22. Write a program in C to input velocity, acceleration and time and calculate distance. s = ut + ½at223. Write a program in C to input three sides of a triangle and calculate area of triangle s=(a+b+c)/2 area = √s × (s – a) × (s – b) × (s – c)  24. Write a program in C to input length, breadth and height and find the area of four walls.25. Write a program in C to input three angles of a triangle and find whether the triangle is an isosceles triangle, right angled triangle, equilateral triangle or scalene triangle.26. Write a program to find prime numbers between two numbers.27. Write a program to ask any number and print the first ten multiples of the ask number.28. Write a program to input the age of a person and find out whether the person is eligible to vote or not.29. Write a program to input a mark in a subject of a student and check if the student has passed or not.30. Write a program to print H.C.F of a given any two numbers.

1. Write a C program to print the message “Welcome to C programming”

#include <stdio.h>
int main() {
    printf("Welcome to C programming\n");
    return 0;
}

2. Write a program to print first 13 odd numbers

#include <stdio.h>

int main() {
    for (int i = 1; i <= 25; i += 2) {
        printf("%d ", i);
    }

    return 0;
}

3. Write a C program to input a number and print the square of a number.

#include <stdio.h>

int main() {
    int number;

    printf("Enter a number: ");

    scanf("%d", &number);

     int square = number * number;

    printf("The square of %d is: %d\n", number, square);

    return 0;
}

4. Write a C program to input a number and print the cube of a number.

#include <stdio.h>

int main() {
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);

    int cube = number * number * number;

    printf("The cube of %d is: %d\n", number, cube);

    return 0;
}

5. Write a C program to input a number and print the square and cube of a number.

#include <stdio.h>

int main() {
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);

    int square = number * number;
    int cube = number * number * number;

    printf("The square of %d is: %d\n", number, square);
    printf("The cube of %d is: %d\n", number, cube);

    return 0;
}

6. Write a C program to input two numbers and find the sum of the numbers.

#include <stdio.h>

int main() {
    int num1, num2;

    printf("Enter first number: ");
    scanf("%d", &num1);

    printf("Enter second number: ");
    scanf("%d", &num2);

    int sum = num1 + num2;

    printf("The sum of %d and %d is: %d\n", num1, num2, sum);

    return 0;
}

7. Write a C program to input two numbers and find the product of the numbers.

#include <stdio.h>

int main() {
    int num1, num2;

    printf("Enter first number: ");
    scanf("%d", &num1);

    printf("Enter second number: ");
    scanf("%d", &num2);

    int product = num1 * num2;

    printf("The product of %d and %d is: %d\n", num1, num2, product);

    return 0;
}

8. Write a C program to input two numbers and find the sum, product and difference of the numbers.

#include <stdio.h>

int main() {
    int num1, num2;

    printf("Enter first number: ");
    scanf("%d", &num1);

    printf("Enter second number: ");
    scanf("%d", &num2);

    int sum = num1 + num2;
    int product = num1 * num2;
    int difference = num1 - num2;

    printf("Sum: %d\n", sum);
    printf("Product: %d\n", product);
    printf("Difference: %d\n", difference);

    return 0;
}

9. Write a program in C to input a length and find the area and perimeter of square.

#include <stdio.h>

int main() {
    float length, area, perimeter;

    printf("Enter the length of the square: ");
    scanf("%f", &length);

    area = length * length;
    perimeter = 4 * length;

    printf("Area of the square: %.2f\n", area);
    printf("Perimeter of the square: %.2f\n", perimeter);

    return 0;
}

10. Write a program in C to input length and breadth and find the area and perimeter of rectangle. 

#include <stdio.h>

int main() {
    float length, breadth, area, perimeter;

    printf("Enter the length of the rectangle: ");
    scanf("%f", &length);

    printf("Enter the breadth of the rectangle: ");
    scanf("%f", &breadth);

    area = length * breadth;
    perimeter = 2 * (length + breadth);

    printf("Area of the rectangle: %.2f\n", area);
    printf("Perimeter of the rectangle: %.2f\n", perimeter);

    return 0;
}

11. Write a program in C to input length, breadth and height and find the volume of box.

#include <stdio.h>

int main() {
    float length, breadth, height, volume;

    printf("Enter the length of the box: ");
    scanf("%f", &length);

    printf("Enter the breadth of the box: ");
    scanf("%f", &breadth);

    printf("Enter the height of the box: ");
    scanf("%f", &height);

    volume = length * breadth * height;

    printf("Volume of the box: %.2f\n", volume);

    return 0;
}

12. Write a program in C to input base and height and find the area of triangle.

#include <stdio.h>

int main() {
    float base, height, area;

    printf("Enter the base of the triangle: ");
    scanf("%f", &base);

    printf("Enter the height of the triangle: ");
    scanf("%f", &height);

    area = 0.5 * base * height;

    printf("Area of the triangle: %.2f\n", area);

    return 0;
}

13. Write a program in C to input principle, rate and time and calculate simple interest.

#include <stdio.h>

int main() {
    float principle, rate, time, simple_interest;

    printf("Enter the principle amount: ");
    scanf("%f", &principle);

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

    printf("Enter the time (in years): ");
    scanf("%f", &time);

    simple_interest = (principle * rate * time) / 100;

    printf("Simple interest: %.2f\n", simple_interest);

    return 0;
}

14. Write a program in C to input radius and find the area of circle (with #define also).

#include <stdio.h>
#define PI 3.14159

int main() {
    float radius, area;

    printf("Enter the radius of the circle: ");
    scanf("%f", &radius);

    area = PI * radius * radius;

    printf("Area of the circle: %.2f\n", area);

    return 0;
}

15. Write a program in C to input radius and find the circumference of circle (with #define ).

#include <stdio.h>
#define PI 3.14159

int main() {
    float radius, circumference;

    printf("Enter the radius of the circle: ");
    scanf("%f", &radius);

    circumference = 2 * PI * radius;

    printf("Circumference of the circle: %.2f\n", circumference);

    return 0;
}

16. Write a program in C to input the diameter and find the area and circumference of the circle. (with #define also)

#include <stdio.h>
#define PI 3.14159

int main() {
    float diameter, radius, area, circumference;

    printf("Enter the diameter of the circle: ");
    scanf("%f", &diameter);

    radius = diameter / 2;
    area = PI * radius * radius;
    circumference = PI * diameter;

    printf("Area of the circle: %.2f\n", area);
    printf("Circumference of the circle: %.2f\n", circumference);

    return 0;
}

17. Write a program in C to input radius and height and find the volume of cylinder. (with #define also)

#include <stdio.h>
#include <math.h>
#define PI 3.14159

int main() {
    float radius, height, volume;

    printf("Enter the radius of the cylinder: ");
    scanf("%f", &radius);

    printf("Enter the height of the cylinder: ");
    scanf("%f", &height);

    volume = PI * pow(radius, 2) * height;

    printf("Volume of the cylinder: %.2f\n", volume);

    return 0;
}

18. Using sqrt function find the square root of an input number.

#include <stdio.h>
#include <math.h>

int main() {
    float number, squareRoot;

    printf("Enter a number: ");
    scanf("%f", &number);

    squareRoot = sqrt(number);

    printf("Square root of %.2f is: %.2f\n", number, squareRoot);

    return 0;
}

19. Using pow function find the square and cube of an input number.

#include <stdio.h>
#include <math.h>

int main() {
    float number, square, cube;

    printf("Enter a number: ");
    scanf("%f", &number);

    square = pow(number, 2);
    cube = pow(number, 3);

    printf("Square of %.2f is: %.2f\n", number, square);
    printf("Cube of %.2f is: %.2f\n", number, cube);

    return 0;
}

20. Write a program to ask if a number is a perfect number or not.

#include <stdio.h>

int isPerfect(int num) {
    int sum = 0;

    for (int i = 1; i <= num / 2; i++) {
        if (num % i == 0) {
            sum += i;
        }
    }

    return sum == num;
}

int main() {
    int num;

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

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

    return 0;
}

21. Using sqrt and pow function find the square root and cube root of a number.

#include <stdio.h>
#include <math.h>

int main() {
    float number, squareRoot, cubeRoot;

    printf("Enter a number: ");
    scanf("%f", &number);

    squareRoot = sqrt(number);
    cubeRoot = cbrt(number);

    printf("Square root of %.2f is: %.2f\n", number, squareRoot);
    printf("Cube root of %.2f is: %.2f\n", number, cubeRoot);

    return 0;
}

22. Write a program in C to input velocity, acceleration and time and calculate distance. s = ut + ½at2

#include <stdio.h>

int main() {
    float velocity, acceleration, time, distance;

    printf("Enter velocity (u): ");
    scanf("%f", &velocity);

    printf("Enter acceleration (a): ");
    scanf("%f", &acceleration);

    printf("Enter time (t): ");
    scanf("%f", &time);

    distance = velocity * time + 0.5 * acceleration * time * time;

    printf("Distance traveled: %.2f\n", distance);

    return 0;
}

23. Write a program in C to input three sides of a triangle and calculate area of triangle s=(a+b+c)/2 area = √s × (s – a) × (s – b) × (s – c) 

#include <stdio.h>
#include <math.h>

int main() {
    float a, b, c, s, area;

    printf("Enter the length of side 'a': ");
    scanf("%f", &a);

    printf("Enter the length of side 'b': ");
    scanf("%f", &b);

    printf("Enter the length of side 'c': ");
    scanf("%f", &c);

    s = (a + b + c) / 2;
    area = sqrt(s * (s - a) * (s - b) * (s - c));

    printf("Area of the triangle: %.2f\n", area);

    return 0;
}

 24. Write a program in C to input length, breadth and height and find the area of four walls. [area=2h(l+b)]

#include <stdio.h>

int main() {
    float length, breadth, height, area;

    printf("Enter the length of the room: ");
    scanf("%f", &length);

    printf("Enter the breadth of the room: ");
    scanf("%f", &breadth);

    printf("Enter the height of the room: ");
    scanf("%f", &height);

    area = 2 * height * (length + breadth);

    printf("Area of the four walls: %.2f\n", area);

    return 0;
}

25. Write a program in C to input three angles of a triangle and find whether the triangle is an isosceles triangle, right angled triangle, equilateral triangle or scalene triangle.

#include <stdio.h>

int main() {
    float angle1, angle2, angle3;

    printf("Enter the first angle: ");
    scanf("%f", &angle1);

    printf("Enter the second angle: ");
    scanf("%f", &angle2);

    printf("Enter the third angle: ");
    scanf("%f", &angle3);

    if (angle1 + angle2 + angle3 == 180) {
        if (angle1 == 90 || angle2 == 90 || angle3 == 90)
            printf("Right-angled triangle\n");
        else if (angle1 == angle2 && angle2 == angle3)
            printf("Equilateral triangle\n");
        else if (angle1 == angle2 || angle2 == angle3 || angle1 == angle3)
            printf("Isosceles triangle\n");
        else
            printf("Scalene triangle\n");
    } else {
        printf("Invalid triangle angles\n");
    }

    return 0;
}

26. Write a program to find prime numbers between two numbers.

#include <stdio.h>

int isPrime(int num) {
    if (num < 2) return 0;
    for (int i = 2; i <= num / 2; i++) {
        if (num % i == 0) {
            return 0;
        }
    }
    return 1;
}

int main() {
    int start, end;
    
    printf("Enter the range (two numbers separated by space): ");
    scanf("%d %d", &start, &end);

    printf("Prime numbers between %d and %d are: ", start, end);
    for (int i = start; i <= end; i++) {
        if (isPrime(i)) {
            printf("%d ", i);
        }
    }

    return 0;
}

27. Write a program to ask any number and print the first ten multiples of the ask number.

#include <stdio.h>

int main() {
    int num;

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

    printf("First ten multiples of %d are: ", num);
    for (int i = 1; i <= 10; i++) {
        printf("%d ", num * i);
    }

    return 0;
}

28. Write a program to input the age of a person and find out whether the person is eligible to vote or not.

#include <stdio.h>

int main() {
    int age;

    printf("Enter the marks: ");
    scanf("%d", &age);

    if (age >= 18) {
        printf("Congratulations! You are eligible to vote.\n");
    } else {
        printf("Sorry, you are not eligible to vote.\n");
    }

    return 0;
}

29. Write a program to input a mark in a subject of a student and check if the student has passed or not.

#include <stdio.h>

int main() {
    int marks;

    printf("Enter the marks: ");
    scanf("%d", &marks);

    if (marks >= 40) {
        printf("Congratulations! You have passed.\n");
    } else {
        printf("Sorry, you have not passed.\n");
    }

    return 0;
}

30. Write a program to print H.C.F of a given any two numbers.

#include <stdio.h>

int findHCF(int a, int b) {
    while (a != b) {
        if (a > b) {
            a -= b;
        } else {
            b -= a;
        }
    }
    return a;
}

int main() {
    int num1, num2;

    printf("Enter two numbers: ");
    scanf("%d %d", &num1, &num2);

    printf("H.C.F. of %d and %d is: %d\n", num1, num2, findHCF(num1, num2));

    return 0;
}

Share This Article
Leave a comment

Leave a Reply

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