How do I create this program for c++ using codeblocks or similiar program? Witho
ID: 3666980 • Letter: H
Question
How do I create this program for c++ using codeblocks or similiar program?
Without using if else if style of coding:
For an entry of 1 calculate the area of a Circle use: Pi r ^2 You should ask for the radius of the circle then display the area and Pi is fixed at 3.14159.
For an entry of 2 calculate the area of a Rectangle use length * width. You should ask for the length and width the display the result.
For an entry of 4 calculate the area of a Triangle use base * height *.5 You should ask for the base and height and display the result.
For an entry of 4 stop the program.
Validate the number entered is between 1 and 4 and display the following statement: Error on selection. Please enter a number from 1 through 4
Validate the input numbers for calculation are positive numbers.
Explanation / Answer
#include <iostream> //this is the header file which includes the I/O codes
using namespace std; //this is written for cout/cin part. This is customary to write in every C++ program
int main() //main function, This is the part where the execution of program starts
{
bool end = false;
int choice;
float radius,length,breadth,base,height,pi = 3.14159;
while(!end)
{
cout<<" MENU DRIVEN PROGRAM ";
cout<<"1. Area of Circle ";
cout<<"2. Area of Rectangle ";
cout<<"3. Area of Triangle ";
cout<<"4. Exit ";
cout<<"Enter your choice::";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter radius of the circle::";
cin>>radius;
if(radius>0)
cout<<"Area of the circle is "<<pi*radius*radius<<endl;
else
cout<<"Radius should be positive ";
break;
case 2:
cout<<"Enter length of the Rectangle::";
cin>>length;
if(length>0)
{
cout<<"Enter breadth of the Rectangle::";
cin>>breadth;
if(breadth>0)
cout<<"Area of the Rectangle is "<<length*breadth<<endl;
else
cout<<"breadth should be positive ";
}else
cout<<"length should be positive ";
break;
case 3:
cout<<"Enter base of the Triangle::";
cin>>base;
if(base>0)
{
cout<<"Enter height of the Triangle::";
cin>>height;
if(height>0)
cout<<"Area of the Triangle is "<<0.5*base*height<<endl;
else
cout<<"height should be positive ";
}else
cout<<"base should be positive ";
break;
case 4:
end = true;
break;
default:
cout<<"Please enter a number from 1 through 4 ";
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.