Computer >> 컴퓨터 >  >> 프로그램 작성 >> C 프로그래밍

C 프로그램은 스위치 케이스를 사용하여 삼각형, 정사각형, 원, 직사각형 및 다각형의 영역을 인쇄합니다.

<시간/>

문제

스위치 케이스를 사용하여 삼각형, 정사각형, 원, 직사각형 및 다각형의 면적을 계산하는 프로그램을 작성하십시오.

해결책

케이스 번호를 기준으로 삼각형, 정사각형, 원, 직사각형 및 다각형의 면적을 계산합니다.

  • 삼각형의 넓이를 찾는 데 사용되는 논리 다음과 같습니다 -

삼각형 a,b,c의 변 입력

s=(float)(a+b+c)/2;
area=(float)(sqrt(s*(s-a)*(s-b)*(s-c)));
  • 제곱 면적을 찾는 데 사용되는 논리 다음과 같습니다 -

런타임 시 정사각형의 측면을 입력합니다.

area=(float)side*side;
  • 원의 면적을 찾는 데 사용되는 논리 다음과 같습니다 -

런타임 시 원의 반지름 입력

area=(float)3.14159*radius*radius;
  • 사각형 면적을 찾는 데 사용되는 논리 다음과 같습니다 -

런타임 시 직사각형의 길이와 너비 입력

area=(float)len*breadth;
  • 평행사변형의 넓이를 구하는 논리는 다음과 같다 -

평행사변형의 밑변과 높이 입력

area=(float)base*height;

예시

다음은 스위치 케이스를 사용하여 삼각형, 정사각형, 원, 직사각형 및 다각형의 면적을 계산하는 C 프로그램입니다 -

#include<stdio.h>
#include<math.h>
main(){
   int choice;
   printf("Enter\n1 to find area of Triangle\n2 for finding area of Square\n3 for finding area of Circle\n4 for finding area of Rectangle\n5 for Parallelogram\n");
   scanf("%d",&choice);
   switch(choice) {
      case 1: {
         int a,b,c;
         float s,area;
         printf("Enter sides of triangle\n");
         scanf("%d%d %d",&a,&b,&c);
         s=(float)(a+b+c)/2;
         area=(float)(sqrt(s*(s-a)*(s-b)*(s-c)));
         printf("Area of Triangle is %f\n",area);
         break;
      }
      case 2: {
         float side,area;
         printf("Enter Sides of Square\n");
         scanf("%f",&side);
         area=(float)side*side;
         printf("Area of Square is %f\n",area);
         break;
      }
      case 3: {
         float radius,area;
         printf("Enter Radius of Circle\n");
         scanf("%f",&radius);
         area=(float)3.14159*radius*radius;
         printf("Area of Circle %f\n",area);
         break;
      }
      case 4: {
         float len,breadth,area;
         printf("Enter Length and Breadth of Rectangle\n");
         scanf("%f %f",&len,&breadth);
         area=(float)len*breadth;
         printf("Area of Rectangle is %f\n",area);
         break;
      }
      case 5: {
         float base,height,area;
         printf("Enter base and height of Parallelogram\n");
         scanf("%f %f",&base,&height);
         area=(float)base*height;
         printf("Enter area of Parallelogram is %f\n",area);
         break;
      }
      default: {
         printf("Invalid Choice\n");
         break;
      }
   }
}

출력

위의 프로그램이 실행되면 다음과 같은 출력을 생성합니다 -

When the above program is executed, it produces the following output:
Run 1:
1 to find area of Triangle
2 for finding area of Square
3 for finding area of Circle
4 for finding area of Rectangle
5 for Parallelogram
5
Enter base and height of Parallelogram
2 4 6 8
Enter area of Parallelogram is 8.000000
Run 2:
1 to find area of Triangle
2 for finding area of Square
3 for finding area of Circle
4 for finding area of Rectangle
5 for Parallelogram
3
Enter Radius of Circle
4.5
Area of Circle is 63.617199