
问题
使用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 −
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 --> Circle2 --> Rectangle3 --> Triangle4 --> Square-------------------------Enter the Figure code3Enter the base and height47Area of a Triangle=14.000000Run 2:-------------------------1 --> Circle2 --> Rectangle3 --> Triangle4 --> Square-------------------------Enter the Figure code1Enter the radius8Area of a circle=201.087997
以上就是使用switch case语句编写的C程序,用于计算几何图形的面积的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1444258.html
微信扫一扫
支付宝扫一扫