Place Your Query
-
C Programming
-
- Control flow-based C Programs
- Enjoy Conditional Programming in C using If...else and switch statement.
- Good, Now write a C program to display "Hello World" on the screen.
- Write a C program to display Your Name, Address and City in different lines.
- C program to find sum of n numbers
- Write a C program For Addition of two numbers using Function.
- Write a Program to Find Simple Interest and Compound Interest.
- Write a Program to Convert Celsius To Fahrenheit and Vice Versa.
- Write a Program to Find Area and Perimeter of Circle, Triangle, Rectangle and Square.
- Write a Program to Swap Two Numbers Using Temporary Variables and Without Using Temporary Variables
- Write a C Program to Design a Simple Menu Driven Calculator
- Simple Expression Based C Programs
-
- 7. Components of C language
- 1. Introduction to C Programming Language
- 10. Operator Precedence and Associativity
- 11. Comments in C Programming
- 14. Fundamental Control Structure Statements in C [Part-1]
- 15. Fundamental Control Structure Statements in C [Part-2]
- 16. Looping Statements [Fundamental Control Structure Statements in C. #Part-3]
- 17. Keyword break, continue, return and exit [Fundamental Control Structure Statements in C. #Part-4]
- 2. Computer Languages
- 3. Interpreters vs Compilers vs Assemblers in programming languages
- 4. C Program Structure
- 5. Compile and Execute C Program
- 6. Errors in C Program
- 8. C Datatypes
- 9. Operators in C
- Control flow-based C Programs
- Demystifying Bit Masking: Unlocking the Power of Bitwise Operators in C-Language with best Examples.
- 18. Fundamentals of C Functions
- Show Remaining Articles (3)Collapse Articles
-
-
Java
-
AI ML
-
FAQs
- A Program to find Tokens in C code?
- What are the Common Coding Mistakes.
- Easy Learning, Python QAs for Beginner’s to Pro Part-1
- Easy Learning, Python QAs for Beginner’s to Pro Part-2
- Easy Learning, Python Strings QAs for Beginner’s to Pro Part-1
- Easy Learning, Python Strings QAs for Beginner’s to Pro Part-2
- Easy Learning, Python String Functions QAs for Beginner to Pro Part-3
-
Python Interview Questions
- Easy Learning, Python QAs for Beginner’s to Pro Part-1
- Easy Learning, Python QAs for Beginner’s to Pro Part-2
- Easy Learning, Python Strings QAs for Beginner’s to Pro Part-1
- Easy Learning, Python Strings QAs for Beginner’s to Pro Part-2
- Easy Learning, Python String Functions QAs for Beginner to Pro Part-3
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
While using in-built C-Compiler Use Gear icon for custom Input (i.e. whenever you have to give values to read)
RESULT
Thus, the C program has been written, executed successfully to find the area and perimeter of circle,
square, rectangle and triangle.