📜  求圆的直径或最长弦

📅  最后修改于: 2021-10-23 08:56:15             🧑  作者: Mango

给定一个半径为“r”的圆,任务是找到圆的直径或最长弦。
例子:

Input: r = 4
Output: 8

Input: r = 9
Output: 18

证明圆的最长弦是它的直径:

  • 在其上画圆 O 和任何弦 AB。
  • 从和弦的一个端点,比如 A,画一条穿过中心的线段。也就是画一个直径。
  • 现在绘制从中心 O 到 B 的半径。
  • 由三角不等式,
AB < AO + OB
 = r + r
 = 2r
 = d
  • 因此,任何不是直径的弦都将小于直径。
  • 所以最大的弦是直径

方法

  • 任何圆的最长弦是它的直径。
  • 因此,圆的直径是其半径的两倍。
Length of the longest chord or diameter = 2r

下面是上述方法的实现:

C++
// C++ program to find
// the longest chord or diameter
// of the circle whose radius is given
 
#include 
using namespace std;
 
// Function to find the longest chord
void diameter(double r)
{
    cout << "The length of the longest chord"
         << " or diameter of the circle is "
         << 2 * r << endl;
}
 
// Driver code
int main()
{
 
    // Get the radius
    double r = 4;
 
    // Find the diameter
    diameter(r);
 
    return 0;
}


Java
// Java program to find
// the longest chord or diameter
// of the circle whose radius is given
class GFG
{
     
// Function to find the longest chord
static void diameter(double r)
{
    System.out.println("The length of the longest chord"
        + " or diameter of the circle is "
        + 2 * r);
}
 
// Driver code
public static void main(String[] args)
{
     
    // Get the radius
    double r = 4;
 
    // Find the diameter
    diameter(r);
}
}
 
// This code contributed by Rajput-Ji


Python3
# Python3 program to find
# the longest chord or diameter
# of the circle whose radius is given
 
# Function to find the longest chord
def diameter(r):
 
    print("The length of the longest chord"
        ," or diameter of the circle is "
        ,2 * r)
 
 
# Driver code
 
# Get the radius
r = 4
 
# Find the diameter
diameter(r)
 
# This code is contributed by mohit kumar


C#
// C# program to find
// the longest chord or diameter
// of the circle whose radius is given
using System;
 
class GFG
{
     
// Function to find the longest chord
static void diameter(double r)
{
    Console.WriteLine("The length of the longest chord"
        + " or diameter of the circle is "
        + 2 * r);
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Get the radius
    double r = 4;
 
    // Find the diameter
    diameter(r);
}
}
 
// This code has been contributed by 29AjayKumar


PHP


Javascript


输出:
The length of the longest chord or diameter of the circle is 8