Find the Diameter or Longest chord of a Circle

07 Jun, 2022
Given a circle with radius ‘r’ is given, the task is to find the diameter or longest chord of the circle.Examples:  
Input: r = 4
Output: 8

Input: r = 9
Output: 18
 
Proof that the Longest chord of a circle is its Diameter:  
Draw circle O and any chord AB on it.
From one endpoint of the chord, say A, draw a line segment through the centre. That is, draw a diameter.
Now draw a radius from centre O to B.
By the triangle inequality,  
AB < AO + OB
 = r + r
 = 2r
 = d
So, any chord that is not a diameter will be smaller than a diameter.
So the largest chord is a diameter
Approach: 
The Longest chord of any circle is its diameter.
Therefore, the diameter of a circle is twice the radius of it. 
Length of the longest chord or diameter = 2r
 
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find// the longest chord or diameter// of the circle whose radius is given #include <bits/stdc++.h>using namespace std; // Function to find the longest chordvoid diameter(double r){    cout << "The length of the longest chord"         << " or diameter of the circle is "         << 2 * r << endl;} // Driver codeint main(){     // Get the radius    double r = 4;     // Find the diameter    diameter(r);     return 0;}
// Java program to find// the longest chord or diameter// of the circle whose radius is givenclass GFG{     // Function to find the longest chordstatic void diameter(double r){    System.out.println("The length of the longest chord"        + " or diameter of the circle is "        + 2 * r);} // Driver codepublic static void main(String[] args){         // Get the radius    double r = 4;     // Find the diameter    diameter(r);}} // This code contributed by Rajput-Ji
# Python3 program to find# the longest chord or diameter# of the circle whose radius is given # Function to find the longest chorddef diameter(r):     print("The length of the longest chord"        ," or diameter of the circle is "        ,2 * r)  # Driver code # Get the radiusr = 4 # Find the diameterdiameter(r) # This code is contributed by mohit kumar
// C# program to find// the longest chord or diameter// of the circle whose radius is givenusing System; class GFG{     // Function to find the longest chordstatic void diameter(double r){    Console.WriteLine("The length of the longest chord"        + " or diameter of the circle is "        + 2 * r);} // Driver codepublic static void Main(String[] args){         // Get the radius    double r = 4;     // Find the diameter    diameter(r);}} // This code has been contributed by 29AjayKumar
<?php// PHP program to find// the longest chord or diameter// of the circle whose radius is given // Function to find the longest chordfunction diameter($r){    echo "The length of the longest chord"        ," or diameter of the circle is "        ,2 * $r << "\n";}     // Driver code    // Get the radius    $r = 4;     // Find the diameter    diameter($r);         // This code is contributed by Ryuga ?>
<script> // javascript program to find// the longest chord or diameter// of the circle whose radius is given  // Function to find the longest chordfunction diameter(r){    document.write("The length of the longest chord"        + " or diameter of the circle is "        + 2 * r);} // Driver code     // Get the radiusvar r = 4; // Find the diameterdiameter(r); // This code contributed by Princi Singh </script>
The length of the longest chord or diameter of the circle is 8
 
Time Complexity: O(1)
Auxiliary Space: O(1)
mohit kumar 29
ankthon
Rajput-Ji
29AjayKumar
princi singh
vansikasharma1329
circle
school-programming
Geometric
Mathematical
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program for distance between two points on earth
Optimum location of point to minimize total distance
Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)
Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
Check whether a given point lies inside a triangle or not
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Merge two sorted arrays