用switch case求几何图形的面积的C程序

问题

使用switch case语句求矩形、正方形、三角形、圆形的面积,用户需要在运行时输入底、高、边、半径、宽和长来计算所有几何图形的面积。

解决方案

下面解释了使用 switch case 语句查找矩形、正方形、三角形、圆形区域的解决方案 -

公式

计算各个几何图形的面积的公式如下 -

  • 长方形的面积=宽*长;

  • 的面积平方=侧*侧;

  • 圆的面积= 3.142*radius*radius;

  • 三角形面积= 0.5 *底*高;

示例

以下是使用 switch case 语句查找矩形、正方形、三角形、圆形区域的 C 程序-

#include <stdio.h>

void main(){

   int fig_code;

   float side, base, length, breadth, height, area, radius;

   printf("-------------------------\n");

   printf(" 1 --> Circle\n");

   printf(" 2 --> Rectangle\n");

   printf(" 3 --> Triangle\n");

   printf(" 4 --> Square\n");

   printf("-------------------------\n");

   printf("Enter the Figure code\n");

   scanf("%d", &fig_code);

   switch(fig_code){

      case 1:

         printf(" Enter the radius\n");

         scanf("%f",&radius);

         area=3.142*radius*radius;

         printf("Area of a circle=%f\n", area);

         break;

      case 2:

         printf(" Enter the breadth and length\n");

         scanf("%f %f",&breadth, &length);

         area=breadth *length;

         printf("Area of a Rectangle=%f\n", area);

         break;

      case 3:

         printf(" Enter the base and height\n");

         scanf("%f %f", &base, &height);

         area=0.5 *base*height;

         printf("Area of a Triangle=%f\n", area);

         break;

      case 4:

         printf(" Enter the side\n");

         scanf("%f", &side);

         area=side * side;

         printf("Area of a Square=%f\n", area);

         break;

      default:

      printf(" Error in figure code\n");

      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程序 的全部内容, 来源链接: utcz.com/z/357441.html

回到顶部