📜  求圆锥和棱锥斜高的程序

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

给定两个代表圆锥高度和半径的整数H1R ,以及代表金字塔底部高度和长度的两个整数H2S ,任务是找到圆锥和金字塔的斜高。

例子:

方法:圆锥或棱锥等物体的倾斜高度是从沿侧面的任何顶点到底部(沿面的中心)测量的距离。直圆锥的斜高在整个表面上是均匀的,由以下公式给出:

金字塔的斜高由以下公式给出:

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to calculate slant
// height of a cone
void coneSlantHeight(double cone_h,
                     double cone_r)
{
    // Store the slant height of cone
    double slant_height_cone
        = sqrt(pow(cone_h, 2)
               + pow(cone_r, 2));
 
    // Print the result
    cout << "Slant height of cone is: "
         << slant_height_cone << '\n';
}
 
// Function to find the slant
// height of a pyramid
void pyramidSlantHeight(double pyramid_h,
                        double pyramid_s)
{
 
    // Store the slant height of pyramid
    double slant_height_pyramid
        = sqrt(pow(pyramid_s / 2, 2)
               + pow(pyramid_h, 2));
 
    // Print the result
    cout << "Slant height of pyramid is: "
         << slant_height_pyramid << '\n';
}
 
// Driver Code
int main()
{
    // Dimensions of Cone
    double H1 = 4.5, R = 6;
 
    // Function Call for slant height
    // of Cone
    coneSlantHeight(H1, R);
 
    // Dimensions of Pyramid
    double H2 = 4, S = 4.8;
 
    // Function to calculate
    // slant height of a pyramid
    pyramidSlantHeight(H2, S);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG
{
     
    // Function to calculate slant
    // height of a cone
    static void coneSlantHeight(double cone_h,
                         double cone_r)
    {
       
        // Store the slant height of cone
        double slant_height_cone
            = Math.sqrt(Math.pow(cone_h, 2)
                   + Math.pow(cone_r, 2));
     
        // Print the result
        System.out.println("Slant height of cone is: " +
        slant_height_cone);
    }
     
    // Function to find the slant
    // height of a pyramid
    static void pyramidSlantHeight(double pyramid_h,
                            double pyramid_s)
    {
     
        // Store the slant height of pyramid
        double slant_height_pyramid
            = Math.sqrt(Math.pow(pyramid_s / 2, 2)
                   + Math.pow(pyramid_h, 2));
     
        // Print the result
        System.out.println("Slant height of pyramid is: " +
        slant_height_pyramid);
    }
     
    // Driver Code
    public static void main (String[] args)
    {
       
        // Dimensions of Cone
        double H1 = 4.5, R = 6;
     
        // Function Call for slant height
        // of Cone
        coneSlantHeight(H1, R);
     
        // Dimensions of Pyramid
        double H2 = 4, S = 4.8;
     
        // Function to calculate
        // slant height of a pyramid
        pyramidSlantHeight(H2, S);
     
    }
}
 
// This code is contributed by AnkThon


Python3
# Python 3 program for the above approach
from math import sqrt,pow
# Function to calculate slant
# height of a cone
def coneSlantHeight(cone_h, cone_r):
  # Store the slant height of cone
  slant_height_cone = sqrt(pow(cone_h, 2) + pow(cone_r, 2))
 
  # Print the result
  print("Slant height of cone is:",slant_height_cone)
 
# Function to find the slant
# height of a pyramid
def pyramidSlantHeight(pyramid_h, pyramid_s):
  # Store the slant height of pyramid
  slant_height_pyramid = sqrt(pow(pyramid_s/2, 2) + pow(pyramid_h, 2))
 
   # Print the result
  print("Slant height of pyramid is:","{:.5f}".format(slant_height_pyramid))
 
# Driver Code
if __name__ == '__main__':
  # Dimensions of Cone
  H1 = 4.5
  R = 6
 
  # Function Call for slant height
  # of Cone
  coneSlantHeight(H1, R);
 
  # Dimensions of Pyramid
  H2 = 4
  S = 4.8
 
  # Function to calculate
  # slant height of a pyramid
  pyramidSlantHeight(H2, S)


C#
// C# program for the above approach
using System;
public class GFG
{
 
  // Function to calculate slant
  // height of a cone
  static void coneSlantHeight(double cone_h,
                              double cone_r)
  {
 
    // Store the slant height of cone
    double slant_height_cone
      = Math.Sqrt(Math.Pow(cone_h, 2)
                  + Math.Pow(cone_r, 2));
 
    // Print the result
    Console.WriteLine("Slant height of cone is: " +
                      slant_height_cone);
  }
 
  // Function to find the slant
  // height of a pyramid
  static void pyramidSlantHeight(double pyramid_h,
                                 double pyramid_s)
  {
 
    // Store the slant height of pyramid
    double slant_height_pyramid
      = Math.Sqrt(Math.Pow(pyramid_s / 2, 2)
                  + Math.Pow(pyramid_h, 2));
 
    // Print the result
    Console.WriteLine("Slant height of pyramid is: " +
                      slant_height_pyramid);
  }
 
  // Driver Code
  public static void Main (string[] args)
  {
 
    // Dimensions of Cone
    double H1 = 4.5, R = 6;
 
    // Function Call for slant height
    // of Cone
    coneSlantHeight(H1, R);
 
    // Dimensions of Pyramid
    double H2 = 4, S = 4.8;
 
    // Function to calculate
    // slant height of a pyramid
    pyramidSlantHeight(H2, S);
 
  }
}
 
// This code is contributed by AnkThon


Javascript


输出:
Slant height of cone is: 7.5
Slant height of pyramid is: 4.66476

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

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