Java Program to Find Area of an Circle Inscribed in a Square

In the previous article, we have discussed about Java Program to Find Area of the Larger Circle when Radius of the Smaller circle and Difference in the Area is Given

In this article we are going to see how to  find area of an circle inscribed in a square using Java programming language.

Java Program to Find Area of an Circle Inscribed in a Square

Before Jumping into the program directly let’s see how to find area of an circle inscribed in a square.

Explanation:

Suppose there is a square having side “a” .

A circle inscribed inside the square will have maximum diameter = a

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

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

Examlpe:

a = 6

r = 6/2 = 3

Ar = pi*r2 = 3.142*9 = 28.278

Let’s see different ways to find area of an circle inscribed in a square.

Method-1: Java Program to Find Area of an Circle Inscribed in a Square By Using Static Input Value

Approach:

  • Declare an double variable say ‘a’ and assign the value to it, which holds the length of the side of the square.
  • Find the radius (say ”r”) of circle using the formula a/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 a = 6;
        // formula to find  radius of the circle
        double r =  a/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 28.278

Method-2: Java Program to Find Area of an Circle Inscribed in a Square By Using User Input Value

Approach:

  • Declare an double variable say ‘a’ and take the value of it as user input, which holds the length of the side of the square.
  • Then we will take the value of “r” as user input using scanner class.
  • Find the radius (say ”r”) of circle using the formula a/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 length of side of the square");
         // to take user input value
        double a = s.nextDouble();                                       
        // formula to find  radius of the circle
        double r =  a/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 length of side of the square
5
The area of the circle is 19.6375

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: