Area of Circle And Calculate Circumference

You are here to learn area of circle and calculate circumference.

Decades ago we have seen and learnt area of circle in our school in geometry class.

I still remember the way how our geometry teacher explained the concept of area of circle using compass and measuring the radius using ruler.

Good’ol days… Now we will see area of a circle in java program.

example: Area of Circle And Calculate Circumference

Area of Circle And Calculate Circumference

// Area of circle is, area = pi * r * r where r is the radius of a circle 

Also Read – Area of Rectangle Program in Java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Area_of_Circle
{
public static void main(String[] args)
{
int radius = 0;
System.out.println("Enter the radius of a Circle: ");
// getting radius from console
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
radius = Integer.parseInt(br.readLine());
}
// To check whether invalid value was entered
catch(NumberFormatException nfe)
{
System.out.println("Invalid radius value" + nfe);
System.exit(0);
}
catch(IOException ioe)
{
System.out.println("IO Error: " + ioe);
System.exit(0);
}
double circleArea = Math.PI * radius * radius;
System.out.println("Area of a circle is " + circleArea);
}
}

Output:
Enter the radius of a Circle: 10
Area of a circle is 314.1592653589793


to calculate circumference: Area of Circle And Calculate Circumference

import java.util.Scanner;

public class CircleCircumference
{
public static void main(String[] args)
{
// Here we are declaring Scanner class which could read from         console
Scanner sc = new Scanner(System.in);
double radius = 0.0;
System.out.println("Please enter radius : ");
radius = sc.nextDouble();
double circumference = 0.0;
// Formula for calculating circumference of circle
// circumference = 2 * 22 / 7 * radius
circumference = 2 * Math.PI * radius; System.out.println("Circumference of circle is : " + circumference);
}
}

Output:

Please enter radius : 5
Circumference of circle is : 31.41592653589793

About the author

admin

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *