C PROGRAM ON AREA AND VOLUME

C Program to Find the Area of a Circle

#include <stdio.h>
//Defining the value of pi upto 6 decimal places
#define pi 3.141592
int main()
{
    float radius;
    printf("Enter Radius of Circle:\n");
    scanf("%f",&radius);
 
    // Calculate the area of the circle
    float area=pi*radius*radius;
 
    // Print the area of the circle, rounded to two decimal places
    printf("The area of Circle with radius %0.2f is: %0.2f",radius,area);
    return 0;
}

 

Runtime Test Cases

Testcase 1: In this case, input the radius as “10” to calculate the area of the circle.

Enter Radius of Circle:

10

The area of Circle with radius 10 is: 314.16

Testcase 2: In this case, input the radius as “11.1” to calculate the area of the circle.

Enter Radius of Circle:

11.1

The area of Circle with radius 11.1 is: 387.08

C Program to Find Area of Parallelogram

/*
 * C Program to Find Area of Parallelogram
 */
#include <stdio.h>
 
int main()
{
    float base, altitude;
    float area;
 
    printf("Enter base and altitude of the given Parallelogram: \n ");
    scanf("%f%f", &base, &altitude);
    area = base * altitude;
    printf("Area of Parallelogram is: %.3f\n", area);
    return 0;
}

 

Runtime Test Cases

 

Output:

$ cc pgm27.c

$ a.out

Enter base and altitude of the given Parallelogram:

 17 19

Area of Parallelogram is: 323.000

C Program to Find the Area of Different Geometrical Shapes

/*
 * C program to find the areas of different geometrical shapes such as
 * circle, square, rectangle etc using switch statements.
 */
#include <stdio.h>
 
void main()
{
    int fig_code;
    float side, base, length, breadth, height, area, radius;
 
    printf("-------------------------\n");
    printf(" 1 --> Circle\n");
    printf(" 2 --> Rectangle\n");
    printf(" 3 --> Triangle\n");
    printf(" 4 --> Square\n");
    printf("-------------------------\n");
    printf("Enter the Figure code\n");
    scanf("%d", &fig_code);
    switch(fig_code)
    {
    case 1:
        printf("Enter the radius\n");
        scanf("%f", &radius);
        area = 3.142 * radius * radius;
        printf("Area of a circle = %f\n", area);
        break;
    case 2:
        printf("Enter the breadth and length\n");
        scanf("%f %f", &breadth, &length);
        area = breadth * length;
        printf("Area of a Reactangle = %f\n", area);
        break;
    case 3:
        printf("Enter the base and height\n");
        scanf("%f %f", &base, &height);
        area = 0.5 * base * height;
        printf("Area of a Triangle = %f\n", area);
        break;
    case 4:
        printf("Enter the side\n");
        scanf("%f", &side);
        area = side * side;
        printf("Area of a Square=%f\n", area);
        break;
    default:
        printf("Error in figure code\n");
        break;
    }
}

 

Runtime Test Cases

 

$ cc pgm77.c

$ a.out

-------------------------

 1 --> Circle

 2 --> Rectangle

 3 --> Triangle

 4 --> Square

-------------------------

Enter the Figure code

1

Enter the radius

30

Area of a circle = 2827.800049

 

$ a.out

-------------------------

 1 --> Circle

 2 --> Rectangle

 3 --> Triangle

 4 --> Square

-------------------------

Enter the Figure code

2

Enter the breadth and length

20 30

Area of a Reactangle = 600.000000

 

$ a.out

-------------------------

 1 --> Circle

 2 --> Rectangle

 3 --> Triangle

 4 --> Square

-------------------------

Enter the Figure code

3

Enter the base and height

45 80

Area of a Triangle = 1800.000000

 

$ a.out

-------------------------

 1 --> Circle

 2 --> Rectangle

 3 --> Triangle

 4 --> Square

-------------------------

Enter the Figure code

4

Enter the side

100

Area of a Square=10000.000000

C Program to Find Volume and Surface Area of Cylinder

/*
 * C Program to Find the Volume and Surface Area of cylinder
 */
#include <stdio.h>
#include <math.h>
 
int main()
{
 
    float radius, height;
    float surface_area, volume;
 
    printf("Enter  value for  radius and height of a cylinder : \n");
    scanf("%f%f", &radius, &height);
    surface_area = 2 * (22 / 7) * radius * (radius + height);
    volume = (22 / 7) * radius * radius * height;
    printf("Surface area of cylinder is: %.3f", surface_area);
    printf("\n Volume of cylinder is : %.3f", volume);
    return 0;
}

 

Runtime Test Cases

 

Output:

$ cc pgm29.c -lm

$ a.out

Enter  value for  radius and height of a cylinder :

15 17

Surface area of cylinder is: 2880.000

Volume of cylinder is : 11475.000

C Program to Find the Perimeter of a Circle, Rectangle and Triangle

*
 * C Program to Find the Perimeter of a Circle, Rectangle and Triangle
 */
#include <stdio.h>
#include <math.h>
 
int main()
{
    float radius, length, width, a, b, c, height;
    int n;
    float perimeter;
 
    //Perimeter of rectangle
    printf(" \n Perimeter of rectangle \n");
    printf("---------------------------\n");
    printf("\n Enter width and length of the rectangle : ");
    scanf("%f%f", &width,& length);
    perimeter = 2 * (width + length);
    printf("Perimeter of rectangle is: %.3f", perimeter);
 
    //Perimeter of triangle
    printf("\n Perimeter of triangle n");
    printf("---------------------------n");
    printf("\n Enter the size of all sides of the triangle : ");
    scanf("%f%f%f", &a, &b, &c);
    perimeter = a + b + c;
    printf("Perimeter of triangle is: %.3f", perimeter);
 
    //Perimeter of circle
    printf(" \n Perimeter of circle \n");
    printf("---------------------------\n");
    printf("\n Enter the radius of the circle : ");
    scanf("%f", &radius);
    perimeter = 2 * (22 / 7) * radius;
    printf("Perimeter of circle is: %.3f", perimeter);
 
    //Perimeter of equilateral triangle
    printf(" \n Perimeter of equilateral triangle \n");
    printf("---------------------------\n");
    printf("\n Enter any side of the equilateral triangle : ");
    scanf("%f", &a);
    perimeter = 3 * a;
    printf("Perimeter of equilateral triangle is: %.3f", perimeter);
 
    //Perimeter of right angled triangle
    printf(" \n Perimeter of right angled triangle \n");
    printf("---------------------------\n");
    printf("\n Enter the width and height of the right angled triangle : ");
    scanf("%f%f", &width, &height);
    perimeter = width + height + sqrt(width * width + height * height);
    printf("Perimeter of right angled triangle is: %.3f", perimeter);
    return 0;
}

Runtime Test Cases

 

Output:

$ cc pgm32.c -lm

$ a.out

 Perimeter of rectangle

---------------------------

 

Enter width and length of the rectangle : 12 13

Perimeter of rectangle is: 50.000

 

Perimeter of triangle

---------------------------

 

Enter the size of all sides of the triangle : 12 16 18

Perimeter of triangle is: 46.000

 

Perimeter of circle

---------------------------

 

Enter the radius of the circle : 10

Perimeter of circle is: 60.000

 

Perimeter of equilateral triangle

---------------------------

 

Enter any side of the equilateral triangle : 19 34

Perimeter of equilateral triangle is: 57.000

 

Perimeter of right angled triangle

---------------------------

 

Enter the width and height of the right angled triangle : 5 7

Perimeter of right angled triangle is: 73.366