Let’s try an exercise this time.
Try to make a program to calculate a rectangle area, or a triangle area, or a circle area.
Make the program to give us three choices, whether we want to calculate one of them.
In the previous post, we have had programs to calculate Rectangle Area, Triangle Area and Circle Area. We can copy them.
Combine with Decision IF Else for more than 2 conditions
Finally it would be seen like the syntax below:
#include "iostream.h"
#include "conio.h"
void main ()
{
float lg,wt,Rarea, bt,hg,Tarea, d,Carea;
int option;
cout << "MAIN MENU";
cout << "\n=========";
cout << "\n 1. Rectangle Area";
cout << "\n 2. Triangle Area";
cout << "\n 3. Circle Area";
cout << "\n===========";
cout << "\n\n Please choose 1, 2 or 3 : ";
cin>>option;
if (option ==1)
{
clrscr();
cout<< "Rectangle Area";
cout<< "\n==============";
cout<< "\n\n Enter long: ";
cin>> lg;
cout<< "Enter width: ";
cin>> wt;
Rarea = lg*wt;
cout<< "\nArea = "<< Rarea;
}
else if (option ==2)
{
clrscr();
cout<< "Triangle Area";
cout<< "\n=============";
cout<< "\n\n Enter long of the bottom: ";
cin>> bt;
cout<< "Enter height: ";
cin>> hg;
Tarea = (bt*hg)*0.5;
cout<< "\nArea = "<< Tarea;
}
else if (option ==3)
{
clrscr();
cout<< "Circle Area";
cout<< "\n===========";
cout<< "\n\nEnter diameter: ";
cin>> d;
Carea = (22*(d/2)*(d/2))/7;
cout<< "\nArea= "<< Carea;
}
else
{ cout << "Wrong option choosed" ;
cout << "\n Please enter a correct one ! ";
}
getch();
}
The result would like the picture below:
1. if we enter number 1, the program will calculate rectangle area
2. if we enter number 2, the program will calculate triangle area
3. if we enter number 3, the program will calculate circle area
0 comments:
Post a Comment