0

0

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

WBOY

WBOY

发布时间:2023-09-02 09:57:02

|

1150人浏览过

|

来源于tutorialspoint

转载

使用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 rectangle, 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 −

Teleporthq
Teleporthq

一体化AI网站生成器,能够快速设计和部署静态网站

下载
  • 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 
void main(){
   int fig_code;
   float side, base, length, breadth, height, area, radius;
   printf("-------------------------

"); printf(" 1 --> Circle

"); printf(" 2 --> Rectangle

"); printf(" 3 --> Triangle

"); printf(" 4 --> Square

"); printf("-------------------------

"); printf("Enter the Figure code

"); scanf("%d", &fig_code); switch(fig_code){ case 1: printf(" Enter the radius

"); scanf("%f",&radius); area=3.142*radius*radius; printf("Area of a circle=%f

", area); break; case 2: printf(" Enter the breadth and length

"); scanf("%f %f",&breadth, &length); area=breadth *length; printf("Area of a Rectangle=%f

", area); break; case 3: printf(" Enter the base and height

"); scanf("%f %f", &base, &height); area=0.5 *base*height; printf("Area of a Triangle=%f

", area); break; case 4: printf(" Enter the side

"); scanf("%f", &side); area=side * side; printf("Area of a Square=%f

", area); break; default: printf(" Error in figure code

"); 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

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
switch语句用法
switch语句用法

switch语句用法:1、Switch语句只能用于整数类型,枚举类型和String类型,不能用于浮点数类型和布尔类型;2、每个case语句后面必须跟着一个break语句,以防止执行其他case的代码块,没有break语句,将会继续执行下一个case的代码块;3、可以在一个case语句中匹配多个值,使用逗号分隔;4、Switch语句中的default代码块是可选的等等。

532

2023.09.21

Java switch的用法
Java switch的用法

Java中的switch语句用于根据不同的条件执行不同的代码块。想了解更多switch的相关内容,可以阅读本专题下面的文章。

412

2024.03.13

length函数用法
length函数用法

length函数用于返回指定字符串的字符数或字节数。可以用于计算字符串的长度,以便在查询和处理字符串数据时进行操作和判断。 需要注意的是length函数计算的是字符串的字符数,而不是字节数。对于多字节字符集,一个字符可能由多个字节组成。因此,length函数在计算字符串长度时会将多字节字符作为一个字符来计算。更多关于length函数的用法,大家可以阅读本专题下面的文章。

920

2023.09.19

高德地图升级方法汇总
高德地图升级方法汇总

本专题整合了高德地图升级相关教程,阅读专题下面的文章了解更多详细内容。

43

2026.01.16

全民K歌得高分教程大全
全民K歌得高分教程大全

本专题整合了全民K歌得高分技巧汇总,阅读专题下面的文章了解更多详细内容。

82

2026.01.16

C++ 单元测试与代码质量保障
C++ 单元测试与代码质量保障

本专题系统讲解 C++ 在单元测试与代码质量保障方面的实战方法,包括测试驱动开发理念、Google Test/Google Mock 的使用、测试用例设计、边界条件验证、持续集成中的自动化测试流程,以及常见代码质量问题的发现与修复。通过工程化示例,帮助开发者建立 可测试、可维护、高质量的 C++ 项目体系。

24

2026.01.16

java数据库连接教程大全
java数据库连接教程大全

本专题整合了java数据库连接相关教程,阅读专题下面的文章了解更多详细内容。

35

2026.01.15

Java音频处理教程汇总
Java音频处理教程汇总

本专题整合了java音频处理教程大全,阅读专题下面的文章了解更多详细内容。

16

2026.01.15

windows查看wifi密码教程大全
windows查看wifi密码教程大全

本专题整合了windows查看wifi密码教程大全,阅读专题下面的文章了解更多详细内容。

56

2026.01.15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号