In the previous article, we have seen Java Program to Find Area of a Circumscribed Circle of a Square
In this article we are going to see how to find center of the circle using endpoints of diameter using Java programming language.
Java Program to Find Center of the Circle Using Endpoints of Diameter
Before Jumping into the program directly let’s see how we can find center of the circle using endpoints of diameter.
Explanation:
Let us assume there is a circle named A
If The diameter of the circle is d,
Then we know that the radius is half of the diameter i.e r=d/2
Now, if the coordinates of the diameter is given as (x1,y1) & (x2,y2)
Then the center of the circle lies exactly in the middle of the diameter,
Hence the coordinate of the center of the circle is (x1+x2)/2 , (y1+y2)/2
Example:
x1= 1 y1= 2 x2= 3 y2= 4 Center coordinate of the circle is = (x1+x2)/2 , (y1+y2)/2 = 2,3
Let’s see different ways to find center of the circle using endpoints of diameter.
Method-1: Java Program to Find Center of the Circle Using Endpoints of Diameter By Using Static Value
Approach:
- Declare an integer variable say ‘
x1
’ & ‘y1
’ and assign the value to it, which holds the coordinate value of one end of the diameter. - Declare an integer variable say ‘
x2
’ & ‘y2
’ and assign the value to it, which holds the coordinate value of the other end of diameter. - Declare an integer variable say “
c
” which will hold the coordinate value of center of the circle using the formula(x1+x2)/2 , (y1+y2)/2
- Print the result.
Program:
import java.io.*; class Main { public static void main(String [] args) { int x1 = 1; int y1 = 2; int x2 = 3; int y2 = 4; int c1 = (x1+x2)/2 ; int c2 = (y1+y2)/2; System.out.println("The center of the circle is " + c1 + "," + c2); } }
Output: The center of the circle is 2,3
Method-2: Java Program to Find Center of the Circle Using Endpoints of Diameter By Using User Input Value
Approach:
- Declare an integer variable say ‘
x1
’ & ‘y1
’ which holds the coordinate value of one end of the diameter. - Declare an integer variable say ‘
x2
’ & ‘y2
’ which holds the coordinate value of the other end of diameter. - Take user input of values of
x1
,y1
,x2
,y2
- Declare an integer variable say “
c
” which will hold the coordinate value of center of the circle using the formula(x1+x2)/2 , (y1+y2)/2
- Print the result.
Program:
import java.io.*; import java.util.Scanner; class Main { public static void main(String [] args) { Scanner s = new Scanner(System.in); System.out.println("Enter the value of x1 coordinate: "); int x1 = s.nextInt(); System.out.println("Enter the value of y1 coordinate: "); int y1 = s.nextInt(); System.out.println("Enter the value of x2 coordinate: "); int x2 = s.nextInt(); System.out.println("Enter the value of y2 coordinate: "); int y2 = s.nextInt(); int c1 = (x1+x2)/2 ; int c2 = (y1+y2)/2; System.out.println("The center of the circle is: " + c1 + "," + c2); } }
Output: Enter the value of x1 coordinate: 4 Enter the value of y1 coordinate: 6 Enter the value of x2 coordinate: 7 Enter the value of y2 coordinate: 9 The center of the circle is: 5,7
Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples
Related Java Articles: