Beta Coder

Learn code like Beta Coder.

Breaking

Sunday, September 13, 2020

Java Program to Calculate Area and Circumference of a Circle

CALCULATE AREA AND CIRCUMFERENCE OF A CIRCLE

JAVA PROGRAM TO FIND AREA AND CIRCUMFERENCE OF CIRCLE


In this program we learn how to calculate area and circumference of a circle based on the radius value provided by user using JAVA.

Area formula of a Circle

The area of a Circle is given by the bellow formula
Area= 3.14 * radius * radius

Circumference formula of a Circle
The circumference of a Circle is given by the bellow formula
Circumference= 2 * 3.14 * radius 
The area and circumference of a circle
Program to compute the area and circumference of a circle 
To compute area and circumference we must know the radius of circle. First user to enter the radius and based on the input it would calculate the values. To make it simple we have taken standard PI value as 3.143 (constant) in the program.  

// FIND AREA AND CIRCUMFERENCE OF CIRCLE
import java.util.*;
class Circle
{
	int r;
	public
		Circle()
		{
			Scanner in = new Scanner(System.in);
			System.out.println("Enter the radius of circle : ");
			r = in.nextInt();
		}
		double area()
		{
			double a;
			a = 3.143 * r * r;
			return(a);
		}
		double circumference()
		{
			double c;
			c = 2 * 3.143 * r;
			return(c);
		}
		void display(double x, double y)
		{
			System.out.println("The area of the circle = "+x+" square unit");
			System.out.println("The circumference of the circle = "+y+" unit");
		}
	public static void main(String arg[])
	{
		Circle p = new Circle();
		double x, y;
		x = p.area();
		y = p.circumference();
		p.display(x, y);
	}
};
Output:
Enter the radius of circle : 6
The area of the circle = 113.09734 square unit
The circumference of the circle : 37.69911 unit

No comments:

Post a Comment