Signup/Sign In
PUBLISHED ON: APRIL 17, 2021

Java Program to Find the Area of A Circle given the Radius

In this tutorial, we will learn how to find the product of two numbers using a recursive function. A recursive function is a function that calls itself. But before moving further, if you are not familiar with the concept of nested if statements in java, then do check the article on the topic Conditional Statement in Java.

Input: Enter the radius of the circle: 7.5

Output: The area of the circle is: 176.78

Program 1: Calculate and Display Area of a Circle

In this program, we will see how to calculate the area of a circle when the radius is given.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare a variable to store the radius.

  4. Ask the user to initialize the variable.

  5. Use the formula to calculate the area of the circle.

  6. Print the result.

  7. Stop.

Let us look at the below program to understand the above algorithm.

//Java Program to find the area of a circle given the radius
import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
        int r;
        double pi = 3.14, area;
        //Take input from the user
        //Create an instance of the Scanner Class
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the radius of circle: ");
        r = sc.nextInt();
        area = pi * r * r;
        System.out.println("The area of the circle: "+area);
    }            
}


Enter the radius of circle: 9
The area of the circle: 254.34

Program 2: Calculate and Display Area of a Circle

In this program, we will see how to calculate the area of a circle when the radius is given using inheritance.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare a variable to store the radius.

  4. Ask the user to initialize the variable.

  5. Create an object of the Main class.

  6. Use inheritance to find the area of the circle.

  7. Print the area of the circle.

  8. Stop

Let us look at the below program to understand the above algorithm.

//Java Program to find the area of a circle given the radius
import java.util.Scanner;
class AreaOfCircle
{
	double area;
	void circle(double rad)
	{
	 area= (22*rad*rad)/7;
	}
}
public class Main extends AreaOfCircle
{
   public static void main(String args[]) 
    {   
      //Take input from the user
      //Create an instance of the Scanner Class    
      Scanner s= new Scanner(System.in);
      System.out.println("Enter the radius of the circle: ");
      double radius= s.nextDouble();      
      Main a=new Main();
      a.circle(radius);
      System.out.println("The area of the circle is: " + a.area);      
   }
 }


Enter the radius of the circle: 5
The area of the circle is: 78.57142857142857



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.