スイッチケースを使用して三角形、正方形、円、長方形、多角形の領域を印刷するCプログラム。
問題
スイッチケースを使用して、三角形、正方形、円、長方形、多角形の面積を計算するプログラムを作成します。
解決策
ケース番号に基づいて、三角形、正方形、円、長方形、多角形の面積が計算されます。
- 三角形の面積を見つけるために使用されるロジック 次のとおりです-
三角形の辺を入力してくださいa、b、c
s=(float)(a+b+c)/2; area=(float)(sqrt(s*(s-a)*(s-b)*(s-c)));
- 正方形の領域を見つけるために使用されるロジック 次のとおりです-
実行時に正方形の辺を入力します。
area=(float)side*side;
- 円の面積を見つけるために使用されるロジック 次のとおりです-
実行時に円の半径を入力してください
area=(float)3.14159*radius*radius;
- 長方形の領域を見つけるために使用されるロジック 次のとおりです-
実行時に長方形の長さと幅を入力します
area=(float)len*breadth;
- 平行四辺形の領域を見つけるために使用されるロジックは次のとおりです-
平行四辺形の底辺と高さを入力
area=(float)base*height;
例
以下は、スイッチケースを使用して三角形、正方形、円、長方形、多角形の面積を計算するCプログラムです-
#include<stdio.h>
#include<math.h>
main(){
int choice;
printf("Enter\n1 to find area of Triangle\n2 for finding area of Square\n3 for finding area of Circle\n4 for finding area of Rectangle\n5 for Parallelogram\n");
scanf("%d",&choice);
switch(choice) {
case 1: {
int a,b,c;
float s,area;
printf("Enter sides of triangle\n");
scanf("%d%d %d",&a,&b,&c);
s=(float)(a+b+c)/2;
area=(float)(sqrt(s*(s-a)*(s-b)*(s-c)));
printf("Area of Triangle is %f\n",area);
break;
}
case 2: {
float side,area;
printf("Enter Sides of Square\n");
scanf("%f",&side);
area=(float)side*side;
printf("Area of Square is %f\n",area);
break;
}
case 3: {
float radius,area;
printf("Enter Radius of Circle\n");
scanf("%f",&radius);
area=(float)3.14159*radius*radius;
printf("Area of Circle %f\n",area);
break;
}
case 4: {
float len,breadth,area;
printf("Enter Length and Breadth of Rectangle\n");
scanf("%f %f",&len,&breadth);
area=(float)len*breadth;
printf("Area of Rectangle is %f\n",area);
break;
}
case 5: {
float base,height,area;
printf("Enter base and height of Parallelogram\n");
scanf("%f %f",&base,&height);
area=(float)base*height;
printf("Enter area of Parallelogram is %f\n",area);
break;
}
default: {
printf("Invalid Choice\n");
break;
}
}
} 出力
上記のプログラムを実行すると、次の出力が生成されます-
When the above program is executed, it produces the following output: Run 1: 1 to find area of Triangle 2 for finding area of Square 3 for finding area of Circle 4 for finding area of Rectangle 5 for Parallelogram 5 Enter base and height of Parallelogram 2 4 6 8 Enter area of Parallelogram is 8.000000 Run 2: 1 to find area of Triangle 2 for finding area of Square 3 for finding area of Circle 4 for finding area of Rectangle 5 for Parallelogram 3 Enter Radius of Circle 4.5 Area of Circle is 63.617199
-
楕円内に内接する正方形内に内接する最大のルーローの三角形?
ここでは、正方形内に内接する最大のルーローの三角形の領域が表示されます。その正方形は、1つの楕円の内側に内接しています。長軸の長さは2a、短軸の長さは2bであることがわかります。正方形の辺は「x」で、ルーローの三角形の高さはhです。 長軸2aと短軸2bの楕円に内接する正方形の辺は-であることがわかります。 ルーローの三角形の高さはaと同じです。したがって、h=xです。したがって、ルーローの三角形の面積は- 。 例 #include <iostream> #include <cmath> using namespace std; float areaRe
-
正方形に内接する円の面積を計算するプログラム
正方形に内接する円は、円の端で円の側面に接する円です。つまり内接円の直径は正方形の辺と同じです。面積は、式「((丌/ 4)* a * a)」を使用して計算できます。 ここで、「a」は正方形の一辺の長さです。 コードのロジック -円の内側に内接する円の面積は、式((丌/ 4)* a * a)を使用して計算されます。 このために、数学的に22/7または3.14であるパイ(丌)の値を定義する必要があります。結果に評価される式は、float変数に保存されます。 例 #include <stdio.h> #include <math.h> int main() { &nb