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++
#include <bits/stdc++.h>
using namespace std;
void diameter( double r)
{
cout << "The length of the longest chord"
<< " or diameter of the circle is "
<< 2 * r << endl;
}
int main()
{
double r = 4;
diameter(r);
return 0;
}
|
Java
class GFG
{
static void diameter( double r)
{
System.out.println( "The length of the longest chord"
+ " or diameter of the circle is "
+ 2 * r);
}
public static void main(String[] args)
{
double r = 4 ;
diameter(r);
}
}
|
Python3
def diameter(r):
print ( "The length of the longest chord"
, " or diameter of the circle is "
, 2 * r)
r = 4
diameter(r)
|
C#
using System;
class GFG
{
static void diameter( double r)
{
Console.WriteLine( "The length of the longest chord"
+ " or diameter of the circle is "
+ 2 * r);
}
public static void Main(String[] args)
{
double r = 4;
diameter(r);
}
}
|
PHP
<?php
function diameter( $r )
{
echo "The length of the longest chord"
, " or diameter of the circle is "
,2 * $r << "\n" ;
}
$r = 4;
diameter( $r );
?>
|
Javascript
<script>
function diameter(r)
{
document.write( "The length of the longest chord"
+ " or diameter of the circle is "
+ 2 * r);
}
var r = 4;
diameter(r);
</script>
|
Output: The length of the longest chord or diameter of the circle is 8
Time Complexity: O(1)
Auxiliary Space: O(1)