- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JAVA Program to Find Length of Longest Chord of a Circle
A circle is a round shape two-dimensional diagram which has no corners. Every circle has an origin point and every point on the circle maintains equal distance from the origin. The distance between the origin and a point in a circle is known as Radius of the circle. And similarly, if we draw a line from one edge to another edge of the circle and the origin is held in the middle of it, that line is known as diameter of the circle. Basically, the diameter is double of the length of the radius.
Chord of the circle refers to a line touching from one endpoint of circle to another end point of circle. Or simply we can say a chord refers to the line whose endpoints lie on the circle. A chord divides the circle into two parts.
As per the problem statement we have to find the longest chord of the circle. It is clear that the line which passes through the origin is the longest chord and it is nothing but the diameter.
If the radius (r) is given, then the diameter of the circle is 2r.
Hence, the longest chord of the circle is 2r. So, let's proceed to see how we can find the longest chord of the circle by using Java programming language.
To show you some instances −
Instance-1
Given radius (r) of the circle is 5 Then the longest chord of the circle = 2r = 2 * 5 = 10
Instance-2
Given radius (r) of the circle is 4.5 Then the longest chord of the circle = 2r = 2 * 4.5 = 9
Instance-3
Given radius (r) of the circle is 4 Then the longest chord of the circle = 2r = 2 * 4 = 8
Algorithm
Step-1 − Get the radius of the circle either by static input or by user input.
Step-2 − Find the diameter of the circle which is nothing but the longest chord of the circle.
Step-3 − Print the result.
Multiple Approaches
We have provided the solution in different approaches.
By Using Static Input Value.
By Using User-Defined Method with Static Input Value.
By Using User-Defined Method with User Input Value.
Let’s see the program along with its output one by one.
Approach-1: By Using Static Input Value
In this approach, we declare a double variable and initialize it with the radius value of the circle. Then by using the algorithm we can find the longest chord of the circle.
Example
import java.util.*; public class Main { //main method public static void main(String[] args) { // radius of the circle double r = 7; //find the longest chord i.e diameter double chord=2*r; //print result System.out.println("Length of longest chord of the circle:"+chord); } }
Output
Length of longest chord of the circle: 14.0
Approach-2: By Using User Defined Method with Static Input Value
In this approach, we declare a double variable and initialize it with the radius value of the circle. Call a user defined method by passing this radius value as parameter. Then inside the method by using the algorithm find the longest chord of the circle.
Example
public class Main { //main method public static void main(String[] args) { //radius of the circle double r = 4; //call the user defined method to find longest chord findLongestChord(r); } //user defined method to find longest chord static void findLongestChord(double r) { //find the longest chord i.e diameter double chord=2*r; //print result System.out.println("Longest chord of the circle: "+chord); } }
Output
Longest chord of the circle: 8.0
Approach-3: By Using User Defined Method with User Input Value
In this approach, we declare a double variable and take the user input of the radius value of the circle. Call a user defined method by passing this radius value as parameter. Then inside the method by using the algorithm find the longest chord of the circle.
Example
import java.util.*; public class Main { //main method public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter radius of circle: "); //take user inout of radius of the circle double r = sc.nextDouble(); //call the user defined method to find longest chord findLongestChord(r); } //user defined method to find longest chord static void findLongestChord(double r) { //find the longest chord i.e diameter double chord=2*r; //print result System.out.println("Length of longest chord of the circle: "+chord); } }
Output
Enter radius of circle: 4.5 Length of longest chord of the circle: 9.0
In this article, we explored how to find the longest chord of a circle in Java by using different approaches.