Java Program to Find Area of the Circle when the Area of Inscribed Square is Given

In the previous article, we have discussed about Java Program to Find Area of an Circle Inscribed in a Square

In this article we are going to see how to find area of the circle when the area of inscribed square is given using Java programming language.

Java Program to Find Area of the Circle when the Area of Inscribed Square is Given

Before Jumping into the program directly let’s see how to find area of the circle when the area of inscribed square is given.

Area of the square is given (say ArSq)

Now, we know need the diagonal of the square “D”= √(2*ArSq)

A circle that inscribed the square will have maximum diameter = D

So the radius of the circle inside the square be “r” = D/2

Hence, area of the circle = pi*r2 = 3.142*(a*a) / 4

Example:

ArSq = 50

D = √(2 * Ar) = 10

r = D/2 = 5

Ar = pi*r2 = 3.142*25 = 78.55

Let’s see different ways to find find area of the circle when the area of inscribed square is given.

Method-1: Java Program to Find Area of the Circle when the Area of Inscribed Square is Given By Using Static Input Value

Approach:

  • Declare an double variable say ‘ArSq’ and assign the value to it, which holds the area of the square.
  • Find the diagonal (say ”D”) of square using the formula √(2 * ArSq)
  • Find the radius of the circle ‘r‘ by using the formula D/2
  • Find the area of the circle using the formula pi*r2
  • Print the result.

Program:

import java.io.*;
class Main
{
    public static void main(String [] args)
    {
        double ArSq = 50;
        // formula to find  diagonal of the square
        double D =  Math.sqrt(2*ArSq);  
        // formula to find  radius of the circle
        double r =  D/2;   
        // formula to find Area of circle
        double Ar =  3.142 * r * r;     
        System.out.println("The area of the circle is " + Ar);
    }
}
Output:

The area of the circle is 78.55

Method-2: Java Program to Find Area of the Circle when the Area of Inscribed Square is Given By Using User Input Value

Approach:

  • Declare an double variable say ‘ArSq’ and take the value as user input, which holds the area of the square.
  • Then we will take the value of “ArSq” as user input using scanner class.
  • Find the diagonal (say ”D”) of square using the formula √(2 * ArSq)
  • Find the radius of the circle ‘r‘ by using the formula D/2
  • Find the area of the circle using the formula pi*r2
  • Print the result.

Program:

import java.io.*;
import java.util.Scanner;
class Main
{
    public static void main(String [] args)
    {
         // scanner class obj ref 
        Scanner s = new Scanner(System.in);                            
        System.out.println("Enter the area of the square");
        // to take user input value
        double ArSq = s.nextDouble();                                   
        // formula to find  diagonal of the square
        double D =  Math.sqrt(2*ArSq);  
        // formula to find  radius of the circle
        double r =  D/2;   
        // formula to find Area of circle
        double Ar =  3.142 * r * r;     
        System.out.println("The area of the circle is " + Ar);     
    }
}

Output:

Enter the area of the square
112.82
The area of the circle is 177.24022

Are you a job seeker and trying to find simple java programs for Interview? This would be the rightchoice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Related Java Programs: