> 백엔드 개발 > C++ > 본문

기하학적 도형의 면적을 계산하기 위해 switch Case 문을 사용하여 작성된 C 프로그램

WBOY
풀어 주다: 2023-09-02 09:57:02
앞으로
1009명이 탐색했습니다.

使用switch case语句编写的C程序,用于计算几何图形的面积

Question

스위치 케이스 문을 사용하여 직사각형, 정사각형, 삼각형 및 원의 면적을 구하세요. 모든 기하학적 도형의 면적을 계산하려면 사용자가 런타임에 밑변, 높이, 변, 반지름, 너비 및 길이를 입력해야 합니다.

Solution

스위치 케이스를 사용하여 직사각형, 정사각형, 삼각형, 원의 면적을 찾는 솔루션 −

공식

각 기하도형의 넓이를 구하는 공식은 다음과 같습니다.

= 변 * 변;
  • Area of ​​​circle = 3.142*radius*radius;
  • Area of ​​​triangle = 0.5 *base*height;
  • Example
  • 다음은 C 프로그램입니다.
  • switch Case 문을 사용하여 직사각형, 정사각형, 삼각형, 원의 영역을 찾으세요
  • Live Demo
#include <stdio.h>
void main(){
   int fig_code;
   float side, base, length, breadth, height, area, radius;
   printf("-------------------------</p><p>");
   printf(" 1 --> Circle</p><p>");
   printf(" 2 --> Rectangle</p><p>");
   printf(" 3 --> Triangle</p><p>");
   printf(" 4 --> Square</p><p>");
   printf("-------------------------</p><p>");
   printf("Enter the Figure code</p><p>");
   scanf("%d", &fig_code);
   switch(fig_code){
      case 1:
         printf(" Enter the radius</p><p>");
         scanf("%f",&radius);
         area=3.142*radius*radius;
         printf("Area of a circle=%f</p><p>", area);
         break;
      case 2:
         printf(" Enter the breadth and length</p><p>");
         scanf("%f %f",&breadth, &length);
         area=breadth *length;
         printf("Area of a Rectangle=%f</p><p>", area);
         break;
      case 3:
         printf(" Enter the base and height</p><p>");
         scanf("%f %f", &base, &height);
         area=0.5 *base*height;
         printf("Area of a Triangle=%f</p><p>", area);
         break;
      case 4:
         printf(" Enter the side</p><p>");
         scanf("%f", &side);
         area=side * side;
         printf("Area of a Square=%f</p><p>", area);
         break;
      default:
      printf(" Error in figure code</p><p>");
      break;
   }
}
로그인 후 복사

Output

위 프로그램을 실행하면 다음과 같은 결과가 나옵니다−

Run 1:
-------------------------
1 --> Circle
2 --> Rectangle
3 --> Triangle
4 --> Square
-------------------------
Enter the Figure code
3
Enter the base and height
4
7

Area of a Triangle=14.000000

Run 2:
-------------------------
1 --> Circle
2 --> Rectangle
3 --> Triangle
4 --> Square
-------------------------
Enter the Figure code
1
Enter the radius
8
Area of a circle=201.087997
로그인 후 복사

위 내용은 기하학적 도형의 면적을 계산하기 위해 switch Case 문을 사용하여 작성된 C 프로그램의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:tutorialspoint.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!