Java Program to Find Equation of Circle From Radius and Center

In the previous article, we have discussed about Java program to Find Radius of Circle When Width and Height of Circle is Given

In this article we are going to see how to find equation of circle from radius and center using Java programming language. Before Jumping into the program directly let’s see how to Find Equation of Circle From radius and center.

Java Program to Find Equation of Circle From Radius and Center

Let the center of the circle be (x1,y1)
Radius of the circle = r
The equation of the circle is

((x-x1)^2)+((y-y1)^2)=r^2
=> (x^2)+(x1^2)-(2*x1*x)+(y^2)+(y1^2)-(2*y1*y)=r^2
=> (x^2) - ( 2*x1*x) + (y^2) - (2*y1+y) = (r^2) - (x1^2) - (y1^2) //arranged

Example:
x1 = 1
y1 = 2
r = 3
Equation of the circle is
(x-1)2 + (y-2)2 = 9
x2 – 2x + y2 – 4y = 4

Let’s see different ways to find equation of circle from radius and center.

Method-1: Java Program to Find Equation of Circle From Radius and Center By Using Static Input Value

Approach:

  • Declare an int variable say ‘x1’ and assign the value to it, which holds the x coordinate of the center of the circle.
  • Declare an int variable say ‘y1’ and assign the value to it, which holds the y coordinate of the center of the circle.
  • Declare an double variable say ‘r’ and assign the value to it, which holds the radius value of the circle.
  • Find the circle equation using the formula (x^2) – ( 2*x1*x) + (y^2) – (2*y1+y) = (r^2) – (x1^2) – (y1^2)
  • Print the result.

Program:

import java.io.*;
class Main
{
    public static void main(String [] args)
    {
        int X1 = 1;
        int Y1 = 2;
        double R = 3;
        System.out.println("The equation of the circle is:");
        System.out.println("(X^2) - (" + 2*X1 + "X) + (Y^2) - (" + 2*Y1 + "Y) =  " + ((R*R) - (X1*X1) - (Y1*Y1)));
    }
}
Output:

The equation of the circle is:
(X^2) - (2X) + (Y^2) - (4Y) = 4.0

Method-2: Java Program to Find Equation of Circle From Radius and Center By Using User Input Value

Approach:

  • Declare an int variable say ‘x1’ which holds the x coordinate of the center of the circle.
  • Declare an int variable say ‘y1’ which holds the y coordinate of the center of the circle.
  • Declare an double variable say ‘r’ and assign the value to it, which it holds the radius value of the circle.
  • Then we will take the value of “x1”, “y1”, “r” as user input using scanner class.
  • Find the circle equation using the formula (x^2) – ( 2*x1*x) + (y^2) – (2*y1+y) = (r^2) – (x1^2) – (y1^2)
  • Print the result.

Program:

class Main
{
    public static void main(String [] args)
    {
         // scanner class obj ref
        Scanner s = new Scanner(System.in);                             
        System.out.println("Enter x1 coordinate of the center of the circle");
        // to take user input value
        int X1 = s.nextInt();                                           
        System.out.println("Enter y1 coordinate of the center of the circle");
        int Y1 =  s.nextInt();
        System.out.println("Enter radius of the circle");
        double R =  s.nextDouble();
        System.out.println("The equation of the circle is:");
        System.out.println("(X^2) - (" + 2*X1 + "X) + (Y^2) - ("+ 2*Y1 + "Y) =  " + ((R*R) - (X1*X1) - (Y1*Y1)));
         
    }
}
Output:

Enter x1 coordinate of the center of the circle
1
Enter y1 coordinate of the center of the circle
2
Enter radius of the circle
3
The equation of the circle is:
(X^2) - (2X) + (Y^2) - (4Y) = 4.0

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.

Related Java Programs: