Translate

Ad-Horizontal

Search This Blog

Tuesday 5 February 2019

How to write a C++ program (Turbo C++) to make a menu driven program and find the area of circle or rectangle or square or triangle or to exit the program.

We are gonna see how to write a C++ program (Turbo C++) to make a menu driven program and find the area of circle or rectangle or square or triangle or to exit the program.



The following codes have been run and compiled in C++(Turbo C++).


The writing in the right-hand side of this "//" in the codes are called comments of the code. They are not a necessary part of the codes and can be omitted in the codes. They are just mentioned so as to tell you the use/purpose of writing the left-hand side of the "//" or the codes.   In some special cases depending upon the Fond & Size of the text on your device the comments may entered the right hand-side of the "//"(next line) and come in between the main codes. so as to, make it easier for you to understand/distinguish the comments from the main codes; we have written the main code and comments in different sizes.
Example: Main codes: "  #include<iostream.h>  ".
                 Comments: "   //It is a header file and used to call the input-output classes(Pre-defined library in C++).  " .


The Variables are user initialized with zero, so that they won't store any garbage value and hamper the final output of the program.

For more accurate result, users may replace "int" and "float" with "long" and "double" respectively.




The code are as follows:


#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
double area=0,rad=0,len=0,bre=0,side=0,a=0,b=0,c=0,s=0;
int ch=0;
cout<<"The menu is as follows:"<<"\n";
cout<<"1. Area of Circle."<<"\n";
cout<<"2. Area of Rectangle."<<"\n";
cout<<"3. Area of Square."<<"\n";
cout<<"4. Area of Triangle."<<"\n";
cout<<"5. Exit."<<"\n";
cout<<"Enter your choice 1-5:"<<"\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter the radius of the circle:"<<"\n";
cin>>rad;
area=3.14*rad*rad;
cout<<"The area of the circle = "<<area<<"\n";
break;
case 2:
cout<<"Enter the length of the rectangle:"<<"\n";
cin>>len;
cout<<"Enter the breadth of the rectangle."<<"\n";
cin>>bre;
area=len*bre;
cout<<"The area of the rectangle = "<<area<<"\n";
break;
case 3:
cout<<"Enter the sides of the square:"<<"\n";
cin>>side
area=side*side;
cout<<"The area of the square = "<<area<<"\n";
break;
case 4:
cout<<"Enter the lengths of the three sides of the triangle, one by one:"<<"\n";
cin>>a>>b>>c;
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"The area of the triangle = "<<area<<"\n";
break;
case 5:
cout<<"You exited the program."<<"\n";
break;
default:
cout<<"Wrong choice!"<<"\n";
break;
}
getch();
}


Program:



Code



Code



Code



Code



Output:


Result



Result



Result



Result



Result







Hope that the Blog have helped you in your work. 


Do visit again to see more Coding related blogs.