C Program to Find the Area of Different Geometrical Shapes

This is a C Program to find the areas of different geometrical figures.

Problem Description

This C Program finds the areas of different geometrical figures.

Problem Solution

The program is menu driven program. This has 4 options that can be chosen by the user. The 4 options are 1) area of circle 2) area of rectangle 3) area of triangle 4) area of square. All the 4 finds area.

Program/Source Code

Here is source code of the C program to find the area of different geometrical figures. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * 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;
    }
}
Program Explanation

In this C program, we are finding the areas of different geometrical figures. First we are displaying the options that can be chosen by the user. Then using switch case statement we are finding the area of the geometrical figures.

advertisement
advertisement

In case1 we are reading the radius of a circle, calculating the area and displaying the result. The following formula is used to calculate the area of a circle
Area = 3.142 * radius * radius

In case2 we are reading the ‘breadth’ and ‘length’ of a rectangle, calculating the area and displaying the result. The following formula is used to calculate the area of a rectangle
Area = breadth * length

In case3 we are reading ‘base’ and ‘height’ of a triangle, calculating the area and displaying the result. The following formula is used to calculate the area of a triangle
Area = 0.5 * base * height

Note: Join free Sanfoundry classes at Telegram or Youtube

In case4 we are reading the side of a square, calculating the area and displaying the result. The following formula is used to calculate the area of a circle
Area = side * side

If the user entered the number not in the menu strip then in default statement then display the output as an error in figure code.

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

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.