Java Program to Find Longest Chord of Circle When Radius is Given

In the previous article, we have discussed about Java Program to Find Length of the Chord of the Circle if Length of Another Chord Which is Equally Inclined through the Diameter is Given

In this article we are going to see how to find longest chord of circle by using Java programming language.

Java Program to Find Longest Chord of Circle When Radius is Given

Before Jumping into the program directly let’s see how we find longest chord of circle.

Suppose there is a circle with radius R is given.

Now, we need to find the longest chord of the circle

We know diameter of a circle is the longest chord of the circle

So D=2*R

Example:

R = 20
D = 40
So longest chord is D whose length is 40.

Let’s see different ways to find longest chord of circle.

Method-1: Java Program to Find Longest Chord of Circle When Radius is Given By Using Static Input Value

Approach:

  • Declare an double variable say ‘R’ and assign the value to it, which holds the radius of the circle.
  • Find the longest chord of circle using the formula 2*R
  • Print the result

Program:

import java.io.*;
class Main
{
    public static void main(String [] args)
    {
        double R = 20;
        // formula to find longest chord of the circle
        double D =  2*R;     
        System.out.println("The longest chord of the circle is " + D);
    }
}
Output:

The longest chord of the circle is 40.0

Method-2: Java Program to Find Longest Chord of Circle When Radius is Given By Using User Input Value

Approach:

  • Declare an double variable say ‘R’ which holds the radius of the circle.
  • Then we will take the value of “R” as user input using scanner class.
  • Find the longest chord of circle using the formula 2*R
  • 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 radius of the circle");
        // to take user input value
        double R = s.nextDouble();                                           
        // formula to find longest chord of the circle
        double D =  2*R;     
        System.out.println("The longest chord of the circle is " + D);
    }
}
Output:

Enter the radius of the circle
15.42
The longest chord of the circle is 30.84

Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.

Related Java Programs: