使用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 −
- 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; } }
输出
当上述程序被执行时,它产生以下结果 −
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中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

本文详细介绍了C函数返回类型,包括基本(int,float,char等),派生(数组,指针,结构)和void类型。 编译器通过函数声明和返回语句确定返回类型,执行

Gulc是一个高性能的C库,优先考虑最小开销,积极的内衬和编译器优化。 其设计非常适合高频交易和嵌入式系统等关键应用程序,其设计强调简单性,模型

本文解释了C函数声明与定义,参数传递(按值和指针),返回值以及常见的陷阱,例如内存泄漏和类型不匹配。 它强调了声明对模块化和省份的重要性

本文详细介绍了字符串案例转换的C功能。 它可以通过ctype.h的toupper()和tolower()解释,并通过字符串迭代并处理零终端。 常见的陷阱,例如忘记ctype.h和修改字符串文字是

本文研究C函数返回值存储。 较小的返回值通常存储在寄存器中以备速度;较大的值可能会使用指针来记忆(堆栈或堆),影响寿命并需要手动内存管理。直接ACC

本文分析了形容词“独特”的多方面用途,探索其语法功能,常见的短语(例如,“不同于”,“完全不同”),以及在正式与非正式中的细微应用

本文解释了C标准模板库(STL),重点关注其核心组件:容器,迭代器,算法和函子。 它详细介绍了这些如何交互以启用通用编程,提高代码效率和可读性t

本文详细介绍了c中有效的STL算法用法。 它强调了数据结构选择(向量与列表),算法复杂性分析(例如,std :: sort vs. std vs. std :: partial_sort),迭代器用法和并行执行。 常见的陷阱
