Write a C program to calculate the area and perimeter of triangle, square, circle and rectangle
by using the switch case statements.(Uses nested switch case statements).
#include <stdio.h>
#include <math.h>
int main()
{
char sel;
int a, b, c, p, pp;
float s;
sel='1';
while (sel != '5'){
printf("\nSelect options: \n");
printf("(1) Area and perimeter of triangle \n");
printf("(2) Area and perimeter of square \n");
printf("(3) Area and perimeter of circle \n");
printf("(4) Area and perimeter of rectangle \n");
printf("(5) exit \n");
printf("\nChoise:\n");
sel = getchar();
switch(sel){
case '1':
printf("\n.........Area and perimeter of triangle........\n\n");
printf("Enter side a: ");
scanf("%d", &a);
printf("Enter side b: ");
scanf("%d", &b);
printf("Enter side c: ");
scanf("%d", &c);
p = a+b+c;
pp = p/2;
s = sqrt(pp*(pp-a)*(pp-b)*(pp-c));
printf("\nArea: %.2f \nPerimetr: %d\n", s, p);
break;
case '2':
printf("\n.........Area and perimeter of square........\n\n");
printf("Enter side square: ");
scanf("%d", &a);
p = (2 * a) + (2 * a);
s = a * a;
printf("\nArea: %.2f \nPerimetr: %d\n", s, p);
break;
case '3':
printf("\n.........Area and perimeter of circle........\n\n");
printf("Enter radius: ");
scanf("%d", &a);
p = 2 * 3.14 * a;
s = 3.14 * (a * a);
printf("\nArea: %.2f \nLength: %d\n", s, p);
break;
case '4':
printf("\n.........Area and perimeter of rectangle........\n\n");
printf("Enter side a: ");
scanf("%d", &a);
printf("Enter side b: ");
scanf("%d", &b);
p = (2 * a) + (2 * b);
s = a * b;
printf("\nArea: %.2f \nPerimetr: %d\n", s, p);
break;
case '5':
break;
}
fflush(stdin);
}
return 0;
}
Comments
Leave a comment