首頁 > 後端開發 > C++ > 主體

使用switch case語句編寫的C程序,用於計算幾何圖形的面積

WBOY
發布: 2023-09-02 09:57:02
轉載
1035 人瀏覽過

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

問題

使用switch case語句找出矩形、正方形、三角形、圓的面積 User need to enter base, height, side, radius, breadth and length at runtime to calculate the areas of all geometrical figures.

Solution

The solution to find the areas of tan

#Solution

The solution to find the areas of tan#Solution

The solution to find the areas of tangle, square, triangle, circle by using the switch case statement is explained below −

    ##Formulae
  • The formulae for finding the areas of the respective geometrical figures are as follows −# geometrical figures are as follows −
  • #Area of​​ rectangle = breadth *length;
  • Area of​​ square = side * side;
  • #Area of​​ circle = 3.142*radius*radius;
Area of​​

triangle

= 0.5 *base*height;

#Example

# Following is the C program to

find the areas of rectangle, square, triangle, circle by using the switch case statement

 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;
   }
}
登入後複製

#################################C ##當上述程式被執行時,它產生以下結果−###
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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板