How to find Area of Ellipse using C++ Programming Language

Area of Ellipse:

Area of ellipse formula is equal to multiplication of width, height and pi (a value of 3.14).

Explanation of Program Code:

First thing is to include library files for input and output streams in the program code, for this include header file iostream or iostream.h . The input stream is cin and output stream is cout. The input stream is to get the value and the output stream is to display to words or you can say that output.Lets start how to design code to calculate for area of ellipse.

Now look at the area of formula , and think how many variables you need and also data type of variables.There are three variables you need, one for width, second for height and third one is to store the result i.e area of ellipse. The data type of variable depends upon the values of width and height. Data type may be integer in case of integer values and double in case of decimal values. But the type of area variable is double because there is decimal value in formula of area which is pi(3.14).

Recommended Article: Read this article for better under standing of program and find what is purpose of this loop.
In order to avoid execution again and again, i used this loop.Because if you think from user point of view, that if there are large number of ellipses and have different values of bases and heights. So its look bad in order to go for execution of code again and again of program code.The purpose of character variable is used in the do while loop. Otherwise it is not necessary if you don't use this loop.I also used statements to add beauty to program code.endl  is for to go to next line.

Program code:

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
double width , height , area;
 char redo;
cout<<"***********************************"<<endl;
cout<<" Area of Ellipse "<<endl;
 cout<<"***********************************"<<endl;

do
{
cout<<"Enter width of ellipse "<<endl;
cin>>width;
cout<<endl<<endl;

cout<<"Enter height of ellipse "<<endl;
cin>>height;
cout<<endl<<endl;

area = 3.14 * width * height ;

cout<<"Area of ellipse is  =  "<<area;
 cout<<endl;
cout<<"****************************************"<<endl;
cout<<"Enter y or Y in case of next calculation = ";
cin>>redo;
cout<<endl<<endl;
}while( redo == 'y' || redo == 'Y');

getch();

}

Output:
***************************************
                       Area of Ellipse    
***************************************                
Enter width of ellipse
2

Enter height of ellipse
2

Area of ellipse is = 12.56
***************************************
Enter y or Y in case of next calculaition  =  

No comments:

Post a Comment

Please disable your ad blocker to support this website.

Our website relies on revenue from ads to keep providing free content.