📜  计算正方形对角线长度的程序

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

给定一个正整数S ,任务是找到边长为S的正方形的对角线长度。

例子:

方法:根据正方形边长与正方形对角线长度之间的数学关系,可以解决给定的问题,如下图所示:

因此,只需使用上面导出的关系计算对角线的长度。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the length of the
// diagonal of a square of a given side
double findDiagonal(double s)
{
    return sqrt(2) * s;
}
 
// Driver Code
int main()
{
    double S = 10;
    cout << findDiagonal(S);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the length of the
// diagonal of a square of a given side
static double findDiagonal(double s)
{
    return (double)Math.sqrt(2) * s;
}
 
// Driver Code
public static void main(String[] args)
{
    double S = 10;
     
    System.out.print(findDiagonal(S));
}
}
 
// This code is contributed by splevel62


Python3
# Python3 program for the above approach
import math
 
# Function to find the length of the
# diagonal of a square of a given side
def findDiagonal(s):
     
    return math.sqrt(2) * s
 
# Driver Code
if __name__ == "__main__":
 
    S = 10
     
    print(findDiagonal(S))
 
# This code is contributed by chitranayal


C#
// C# program for the above approach
using System;
public class GFG
{
 
// Function to find the length of the
// diagonal of a square of a given side
static double findDiagonal(double s)
{
    return (double)Math.Sqrt(2) * s;
}
 
// Driver Code
public static void Main(String[] args)
{
    double S = 10;
     
    Console.Write(findDiagonal(S));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
14.1421

时间复杂度: O(1)
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程