সমস্যা
সুইচ কেস ব্যবহার করে ত্রিভুজ, বর্গ, বৃত্ত, আয়ত এবং প্যারালেলোগ্রামের ক্ষেত্রফল বের করার প্রোগ্রাম লিখুন।
সমাধান
কেস নম্বরের ভিত্তিতে ত্রিভুজ, বর্গ, বৃত্ত, আয়ত এবং প্যারালেলোগ্রামের ক্ষেত্রফল ক্যালকুলেট করা হয়।
- ত্রিভুজের ক্ষেত্রফল বের করার লজিক নিম্নরূপ:
ত্রিভুজের তিন বাহু 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