Write a C program to implement menu driven calculator to find the area and perimeter of circle, triangle, rectangle, square, trapezium, rhombus and parallelogram.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
float area, perimeter, s;
int a, b, c, length, breath, height, radius, side, ch;
while(1) {
/* get the option from user */
printf("1. Circle\t2. Triangle\n");
printf("3. Rectangle\t4. Square\n");
printf("5. Trapezoid\t6. Rhombus\n");
printf("7. Parallelogram\t8. Exit\n");
printf("Enter your choice:");
scanf("%d", &ch);
switch(ch) {
case 1:
/* area and circumference of circle */
printf("Enter the radius of the circle:");
scanf("%d", &radius);
area = 3.14 * radius * radius;
perimeter = 2 * 3.14 * radius;
printf("Area of Circle: %f\n", area);
printf("Circumference of Circle: %f\n", perimeter);
break;
case 2:
/* area and perimeter of triangle */
printf("Enter the sides of the triangle:");
scanf("%d%d%d", &a, &b, &c);
s = (1.0 * (a + b + c)) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
perimeter = 2 * s;
printf("Area of Triangle: %f\n", area);
printf("Perimeter of Triangle: %f\n", perimeter);
break;
Note:
gcc calc.c -lm => link math library since we have use math function sqrt().
#include <stdlib.h>
#include <math.h>
int main() {
float area, perimeter, s;
int a, b, c, length, breath, height, radius, side, ch;
while(1) {
/* get the option from user */
printf("1. Circle\t2. Triangle\n");
printf("3. Rectangle\t4. Square\n");
printf("5. Trapezoid\t6. Rhombus\n");
printf("7. Parallelogram\t8. Exit\n");
printf("Enter your choice:");
scanf("%d", &ch);
switch(ch) {
case 1:
/* area and circumference of circle */
printf("Enter the radius of the circle:");
scanf("%d", &radius);
area = 3.14 * radius * radius;
perimeter = 2 * 3.14 * radius;
printf("Area of Circle: %f\n", area);
printf("Circumference of Circle: %f\n", perimeter);
break;
case 2:
/* area and perimeter of triangle */
printf("Enter the sides of the triangle:");
scanf("%d%d%d", &a, &b, &c);
s = (1.0 * (a + b + c)) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
perimeter = 2 * s;
printf("Area of Triangle: %f\n", area);
printf("Perimeter of Triangle: %f\n", perimeter);
break;
case 3:
/* area and perimeter of rectangle */
printf("Enter the length and breath of Rectangle:");
scanf("%d%d", &length, &breath);
area = length * breath;
perimeter = 2 * (length + breath);
printf("Area of Rectangle: %f\n", area);
printf("Perimeter of Rectangle: %f\n", perimeter);
break;
case 4:
/* area and perimeter of square */
printf("Enter the side of a square:");
scanf("%d", &side);
area = side * side;
perimeter = 4 * side;
printf("Area of square: %f\n", area);
printf("Perimeter of Square: %f\n", perimeter);
break;
case 5:
/* area of trapezoid */
printf("Enter the length of parallel sides(a & b):");
scanf("%d%d", &a, &b);
printf("Enter the height:");
scanf("%d", &height);
area = ((a + b) * 1.0 * height) / 2;
printf("Area of trapezoid: %f\n", area);
break;
case 6:
/* area of rhombus */
printf("Enter the diagonal values(p & q):");
scanf("%d%d", &a, &b);
area = a * b / 2;
printf("Area of rhombus: %f\n", area);
break;
case 7:
/* area and perimeter of parallelogram */
printf("Enter the breath and height of parallelogram:");
scanf("%d%d", &b, &height);
area = b * height * 1.0 / 2;
perimeter = (2.0 * (b + height));
printf("Area of parallelogram: %f\n", area);
printf("Perimeter of parallelogram: %f\n", perimeter);
break;
case 8:
exit(0);
default:
printf("Wrong Option!!\n");
break;
}
}
return 0;
}
Note:
gcc calc.c -lm => link math library since we have use math function sqrt().
Output:
jp@jp-VirtualBox:~/$ ./a.out
1. Circle 2. Triangle
3. Rectangle 4. Square
5. Trapezoid 6. Rhombus
7. Parallelogram 8. Exit
Enter your choice:1
Enter the radius of the circle:10
Area of Circle: 314.000000
Circumference of Circle: 62.799999
1. Circle 2. Triangle
3. Rectangle 4. Square
5. Trapezoid 6. Rhombus
7. Parallelogram 8. Exit
Enter your choice:2
Enter the sides of the triangle:5 6 2
Area of Triangle: 4.683749
Perimeter of Triangle: 13.000000
1. Circle 2. Triangle
3. Rectangle 4. Square
5. Trapezoid 6. Rhombus
7. Parallelogram 8. Exit
Enter your choice:3
Enter the length and breath of Rectangle:10 20
Area of Rectangle: 200.000000
Perimeter of Rectangle: 60.000000
1. Circle 2. Triangle
3. Rectangle 4. Square
5. Trapezoid 6. Rhombus
7. Parallelogram 8. Exit
Enter your choice:4
Enter the side of a square:10
Area of square: 100.000000
Perimeter of Square: 40.000000
1. Circle 2. Triangle
3. Rectangle 4. Square
5. Trapezoid 6. Rhombus
7. Parallelogram 8. Exit
Enter your choice:5
Enter the length of parallel sides(a & b):10 20
Enter the height:10
Area of trapezoid: 150.000000
1. Circle 2. Triangle
3. Rectangle 4. Square
5. Trapezoid 6. Rhombus
7. Parallelogram 8. Exit
Enter your choice:6
Enter the diagonal values(p & q):10 10
Area of rhombus: 50.000000
1. Circle 2. Triangle
3. Rectangle 4. Square
5. Trapezoid 6. Rhombus
7. Parallelogram 8. Exit
Enter your choice:7
Enter the breath and height of parallelogram:10 20
Area of parallelogram: 100.000000
Perimeter of parallelogram: 60.000000
1. Circle 2. Triangle
3. Rectangle 4. Square
5. Trapezoid 6. Rhombus
7. Parallelogram 8. Exit
Enter your choice:8
1. Circle 2. Triangle
3. Rectangle 4. Square
5. Trapezoid 6. Rhombus
7. Parallelogram 8. Exit
Enter your choice:1
Enter the radius of the circle:10
Area of Circle: 314.000000
Circumference of Circle: 62.799999
1. Circle 2. Triangle
3. Rectangle 4. Square
5. Trapezoid 6. Rhombus
7. Parallelogram 8. Exit
Enter your choice:2
Enter the sides of the triangle:5 6 2
Area of Triangle: 4.683749
Perimeter of Triangle: 13.000000
1. Circle 2. Triangle
3. Rectangle 4. Square
5. Trapezoid 6. Rhombus
7. Parallelogram 8. Exit
Enter your choice:3
Enter the length and breath of Rectangle:10 20
Area of Rectangle: 200.000000
Perimeter of Rectangle: 60.000000
1. Circle 2. Triangle
3. Rectangle 4. Square
5. Trapezoid 6. Rhombus
7. Parallelogram 8. Exit
Enter your choice:4
Enter the side of a square:10
Area of square: 100.000000
Perimeter of Square: 40.000000
1. Circle 2. Triangle
3. Rectangle 4. Square
5. Trapezoid 6. Rhombus
7. Parallelogram 8. Exit
Enter your choice:5
Enter the length of parallel sides(a & b):10 20
Enter the height:10
Area of trapezoid: 150.000000
1. Circle 2. Triangle
3. Rectangle 4. Square
5. Trapezoid 6. Rhombus
7. Parallelogram 8. Exit
Enter your choice:6
Enter the diagonal values(p & q):10 10
Area of rhombus: 50.000000
1. Circle 2. Triangle
3. Rectangle 4. Square
5. Trapezoid 6. Rhombus
7. Parallelogram 8. Exit
Enter your choice:7
Enter the breath and height of parallelogram:10 20
Area of parallelogram: 100.000000
Perimeter of parallelogram: 60.000000
1. Circle 2. Triangle
3. Rectangle 4. Square
5. Trapezoid 6. Rhombus
7. Parallelogram 8. Exit
Enter your choice:8
No comments:
Post a Comment