Write a Program to Find Area and Perimeter of Circle, Triangle, Rectangle and Square.

Table of Contents
< All Topics
Print

Write a Program to Find Area and Perimeter of Circle, Triangle, Rectangle and Square.

Aim

Write a C program that calculates the area and perimeter of various geometric shapes, including circles, triangles, rectangles, and squares. Using the program menu Enter the shape you want to calculate the area and perimeter of: 1. for Circle 2. for Square 3. for Rectangle and 4 for Triangle.

Procedure

Step-1: Start the program execution.

Step-2: Declare the variables ch, a, b, c, h, r.

Step-3: Get the choice whether 1. circle 2. square 3. rectangle and 4. triangle.

Step-4: If ch=1, read r and compute area and perimeter of circle.

Step-5: Print the area and perimeter of circle.

Step-6: If ch=2, read a and compute area and perimeter of square.

Step-7: Print the area and perimeter of square.

Step-8: If ch=3, read b, h and compute area and perimeter of rectangle.

Step-9: Print the area and perimeter of rectangle.

Step-10: If ch=4, read a, b, c and compute area and perimeter of triangle.

Step-11: Print the area and perimeter of triangle.

Step-12: Stop the program execution.

Coding

#include<stdio.h>
#include<conio.h>

// author milind bhatt
int main()
{
int ch,b,h,a,c;
float r;

printf("Enter the choice:\n1.Circle\n2.Square\n3.Rectangle\n4.Triangle\nChoice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the radius of the circle:");
scanf("%f",&r);

printf("\nArea of circle=%f",3.14*r*r);
printf("\nPerimeter of circle=%f",2*3.14*r);
break;

case 2: printf("\nEnter the side of the square:");
scanf("%d",&a);

printf("\nThe area of the square is %d",a*a);
printf("\nPerimeter of square=%d",4*a);
break;

case 3: printf("\nEnter the breadth and height of rectangle:");
scanf("%d%d",&b,&h);

printf("\nArea of the rectangle:%d",b*h);
printf("\nPerimeter of the rectangle:%d",2*(b+h));
break;

case 4: printf("\nEnter the breadh and height of triangle:");
scanf("%d%d",&b,&h);
printf("\nEnter three sides of triangle:");
scanf("%d%d%d",&a,&b,&c);
printf("\nArea of triangle:%f",(float)(0.5*b*h));
printf("\nPerimeter of triangle:%f",(float)(a+b+c));
break;

default:printf("\n Enter the correct choice:");

}// end switch
return 0;
}// end main

Output

ROUND-1
--------------

Enter the choice:
1.Circle
2.Square
3.Rectangle
4.Triangle
Choice:1

Enter the radius of the circle:10

Area of circle=314.000000
Perimeter of circle=62.800000
--------------------------------

ROUND-2
--------------
Enter the choice:
1.Circle
2.Square
3.Rectangle
4.Triangle
Choice:2

Enter the side of the square:10

The area of the square is 100
Perimeter of square=40
--------------------------------

ROUND-3
--------------
Enter the choice:
1.Circle
2.Square
3.Rectangle
4.Triangle
Choice:3

Enter the breadth and height of rectangle:
12
20

Area of the rectangle:240
Perimeter of the rectangle:64
--------------------------------

ROUND-4
--------------
Enter the choice:
1.Circle
2.Square
3.Rectangle
4.Triangle
Choice:4

Enter the breadh and height of triangle:54
20

Enter three sides of triangle:5
10
17

Area of triangle:100.000000
Perimeter of triangle:32.000000
--------------------------------

Run Directly on in-built C-Compiler

RESULT

Thus, the C program has been written, executed successfully to find the area and perimeter of circle,

square, rectangle and triangle.


Recommended Readings

Leave a comment