Java Program To Calculate And Display The Area Of An Ellipse

Java Program To Calculate And Display The Area Of An Ellipse

In this tutorial we will learn how to create a Java Program to Calculate the Area of an Ellipse.

Area of an ellipse: The formula to find the area of an ellipse is given below:
Area = pi * a * b

where:
---------
--> a is the distance from the center to a vertex.
--> b is the distance from the center to a co-vertex.

Example of Area of an Ellipse:
In the ellipse below a is 6 and b is 2, so the area of ellipse is 12*3.14


Program 1: Using Java Scanner Class

/*
Algorithm:
-------------
1. Get the value of a from the user.
2. Get the value of b from the user.
3. Calculate the Area of a Ellipse.
4. Print the Area.
*/

//importing java Scanner package
import java.util.Scanner;

public class AreaOfEllipse1{
public static void main (String[] args){
//creating new Scanner
Scanner sc = new Scanner (System.in);

System.out.println ("Please enter the value of a: ");
double a = sc.nextDouble();

System.out.println ("Please enter the value of b: ");
double b = sc.nextDouble();

//calculate the area of ellipse
double pi = 3.14;
double area = pi * a * b;

//print the area
System.out.println ("Area of Ellipse :: "+area);
}
}

Save this file as 'AreaOfEllipse1.java'
Compile: $javac AreaOfEllipse1.java
Execute: $java AreaOfEllipse1
Output:

Please enter the value of a:
6
Please enter the value of b:
2
Area of Ellipse :: 37.68

Program 2: Using Java BufferedReader Class

/*
Algorithm:
-------------
1. Get the value of a from the user.
2. Get the value of b from the user.
3. Calculate the Area of a Ellipse.
4. Print the Area.
*/

// importhing java packages
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class AreaOfEllipse2{
public static void main (String[] args)
throws IOException{

//creating new InputSreamReader
InputStreamReader is = new InputStreamReader (System.in);

//creating new BufferedReader
BufferedReader br = new BufferedReader (is);

System.out.println ("Please enter the value of a: ");
double a = Double.parseDouble (br.readLine());

System.out.println ("Please enter the value of b: ");
double b = Double.parseDouble (br.readLine());

//calculate the area of ellipse
double pi = 3.14;
double area = pi * a * b;

//print the area
System.out.println ("Area of the Ellipse :: "+area);
}
}

Save this file as 'AreaOfEllipse2.java'
Compile: $javac AreaOfEllipse2.java
Execute: $java AreaOfEllipse2
Output:

Please enter the value of a:
12.10
Please enter the value of b:
6.15
Area of the Ellipse :: 233.66310000000001










Post a Comment

0 Comments