ホームページ  >  記事  >  バックエンド開発  >  switch case文を使って幾何図形の面積を計算するCプログラム

switch case文を使って幾何図形の面積を計算するCプログラム

WBOY
WBOY転載
2023-09-02 09:57:02988ブラウズ

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

質問

switch case ステートメントを使用して、長方形、正方形、三角形、円の面積を求める すべての幾何学的図形の面積を計算するには、ユーザーは実行時に底辺、高さ、辺、半径、幅、長さを入力する必要があります。

ソリューション

長方形、正方形、長方形の面積を求めるソリューションswitch case ステートメントを使用した三角形、円については、以下で説明します。 −

各幾何図形の面積を求める式は次のとおりです。 −

  • 長方形の面積 = 幅 * 長さ;
  • 正方形の面積 = 辺 * 辺;
  • ## の面積 #circle = 3.142*半径*半径;
  • 三角形の面積 = 0.5 *底辺*高さ;
  • #Example

以下は、switch case ステートメントを使用して、

長方形、正方形、三角形、円の面積を求める C プログラムです。

ライブ デモ

#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;
   }
}

出力

上記のプログラムを実行すると、次の結果が生成されます -

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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。