**Java Basic Program**

➤ Hello World program

➤ Java Addition Program

➤ Average of 2 Numbers

➤ Average of 3 Numbers

➤ Simple Interest in Java

➤ Compound Interest

➤ Display ASCII Value

➤ Area of Circle Java

➤ Area of Rectangle

➤ Java Area of Triangle

➤ Java Swap 2 Numbers

➤ Distance b/w 2 Points

**Java Flow Control Program**

**Java Array Programs**

**Java String Programs**

In this tutorial, we will develop a Java program to calculate the area of the circle. The area of the circle is given as **ℼ*radius*radius.**

Procedure to develop a Java program to calculate that area of the circle:-

1) Define a class and main method

2) Declare a variable for taking input:- radius

3) Import Scanner class of util package to read input

4) Read input from end-user and store them in the declared variable

5) Calculate area using the formula, and store it in a variable

6) Display the result

7) Close the Scanner class object

```
import java.util.Scanner;
public class CircleArea {
public static void main(String[] args) {
// declare variables
double radius = 0.0;
double area = 0.0;
// create Scanner class object to read input
Scanner scan = new Scanner(System.in);
// read input
System.out.print("Enter the radius of circle:: ");
radius = scan.nextDouble();
// calculate area
area = Math.PI * radius * radius;
// display result
System.out.println("Area of circle = "+area);
// close Scanner class object
scan.close();
}
}
```

Output:-

Enter the radius of circle:: 9.5

Area of circle = 283.5287369864788

## Program explanation

The radius of the circle may be an integer or floating-point number, so it is a better idea to take the radius variable as the double data type. To read the double data type number, we need to use the nextDouble() method.

To find the radius of the circle the PI value should be used in the program. You can write the value of ℼ explicitly in the program or you can use the pre-defined variable PI. In Math class of java.lang package, the PI variable is defined as the static variable, and it holds the value of ℼ. To use that PI variable we should call them with the class name as Math.PI

The area of circle calculated as PI*radius*radius. Therefore, we can also use the ** pow(double a, double b)** method of Math class. The pow(double a, double b) of Math class is a static method. So, to call the pow(-,-) method no need to create an object of the Math class, just use Math.pow(-,-).

The below lines gives the same result,

```
// calculate area
area = Math.PI * radius * radius;
Or use,
area = Math.PI * Math.pow(radius, 2);
```

Now, after calculating the area of the circle we displayed it using println() method. But, you can observe that it gives results very long as “283.5287369864788”. If you want to display output in short form then you can use printf() method. The printf() method in Java works similarly as it works in C because it is not the standard method of Java.

`System.out.printf("Area of circle = %.2f", area);`

Now, it will display the output in only two precision after the decimal point. Learn more:- output formatting using printf()

Output:-

Enter the radius of the circle:: 9.5

Area of circle = 283.53

## Java program to calculate the area of circle using method

We had developed a java program to find the area of circle by writing all the logic in the main method. It is the wrong way, we should write the business logic inside the separate method and from the main method we should call them. Remaining all the things will remain the same in the program.

```
import java.util.Scanner;
public class CircleArea {
public static double findCircleArea(double radius) {
return Math.PI * Math.pow(radius, 2);
}
public static void main(String[] args) {
// declare variables
double radius = 0.0;
double area = 0.0;
// create Scanner class object to read input
Scanner scan = new Scanner(System.in);
// read input
System.out.print("Enter the radius of circle:: ");
radius = scan.nextDouble();
// calculate area by calling findCircleArea() method
area = findCircleArea(radius);
// display result
System.out.printf("Area of circle = %.2f", area);
// close Scanner class object
scan.close();
}
}
```

Output:-

Enter the radius of circle:: 50

Area of circle = 7853.98

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!