Program to find slant height of cone and pyramid
Last Updated :
07 Aug, 2022
Given two integers H1 and R representing the height and radius of a cone and two integers H2 and S representing the height and length of base of a pyramid, the task is to find the slant height of the cone and the pyramid.
Examples:
Input: H1 = 4.5, R = 6, H2 = 4, S = 4.8
Output:
Slant height of cone is: 7.5
Slant height of pyramid is: 4.66476
Input: H1 = 2, R = 4, H2 = 4, S = 8
Output:
Slant height of cone is: 4.47214
Slant height of pyramid is: 5.65685
Approach: The slant height of an object such as a cone or a pyramid is the distance measured from any vertex along a lateral face to the base (along the center of the face). The slant height of a right circular cone is uniform throughout the surface and is given by the formula:
where,
L is the slant height of the right circular cone
R is the radius of the right circular cone and
H is the height of the right circular cone
The slant height of a pyramid is given by the formula:
where,
L is the slant height of the pyramid
S is the side of the base of the pyramid
H is the height of the pyramid
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void coneSlantHeight( double cone_h,
double cone_r)
{
double slant_height_cone
= sqrt ( pow (cone_h, 2)
+ pow (cone_r, 2));
cout << "Slant height of cone is: "
<< slant_height_cone << '\n' ;
}
void pyramidSlantHeight( double pyramid_h,
double pyramid_s)
{
double slant_height_pyramid
= sqrt ( pow (pyramid_s / 2, 2)
+ pow (pyramid_h, 2));
cout << "Slant height of pyramid is: "
<< slant_height_pyramid << '\n' ;
}
int main()
{
double H1 = 4.5, R = 6;
coneSlantHeight(H1, R);
double H2 = 4, S = 4.8;
pyramidSlantHeight(H2, S);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void coneSlantHeight( double cone_h,
double cone_r)
{
double slant_height_cone
= Math.sqrt(Math.pow(cone_h, 2 )
+ Math.pow(cone_r, 2 ));
System.out.println( "Slant height of cone is: " +
slant_height_cone);
}
static void pyramidSlantHeight( double pyramid_h,
double pyramid_s)
{
double slant_height_pyramid
= Math.sqrt(Math.pow(pyramid_s / 2 , 2 )
+ Math.pow(pyramid_h, 2 ));
System.out.println( "Slant height of pyramid is: " +
slant_height_pyramid);
}
public static void main (String[] args)
{
double H1 = 4.5 , R = 6 ;
coneSlantHeight(H1, R);
double H2 = 4 , S = 4.8 ;
pyramidSlantHeight(H2, S);
}
}
|
Python3
from math import sqrt, pow
def coneSlantHeight(cone_h, cone_r):
slant_height_cone = sqrt( pow (cone_h, 2 ) + pow (cone_r, 2 ))
print ( "Slant height of cone is:" ,slant_height_cone)
def pyramidSlantHeight(pyramid_h, pyramid_s):
slant_height_pyramid = sqrt( pow (pyramid_s / 2 , 2 ) + pow (pyramid_h, 2 ))
print ( "Slant height of pyramid is:" , "{:.5f}" . format (slant_height_pyramid))
if __name__ = = '__main__' :
H1 = 4.5
R = 6
coneSlantHeight(H1, R);
H2 = 4
S = 4.8
pyramidSlantHeight(H2, S)
|
C#
using System;
public class GFG
{
static void coneSlantHeight( double cone_h,
double cone_r)
{
double slant_height_cone
= Math.Sqrt(Math.Pow(cone_h, 2)
+ Math.Pow(cone_r, 2));
Console.WriteLine( "Slant height of cone is: " +
slant_height_cone);
}
static void pyramidSlantHeight( double pyramid_h,
double pyramid_s)
{
double slant_height_pyramid
= Math.Sqrt(Math.Pow(pyramid_s / 2, 2)
+ Math.Pow(pyramid_h, 2));
Console.WriteLine( "Slant height of pyramid is: " +
slant_height_pyramid);
}
public static void Main ( string [] args)
{
double H1 = 4.5, R = 6;
coneSlantHeight(H1, R);
double H2 = 4, S = 4.8;
pyramidSlantHeight(H2, S);
}
}
|
Javascript
<script>
function coneSlantHeight( cone_h,
cone_r)
{
var slant_height_cone =
Math.sqrt(Math.pow(cone_h, 2) +
Math.pow(cone_r, 2));
document.write( "Slant height of cone is: "
+ slant_height_cone + "<br>" );
}
function pyramidSlantHeight( pyramid_h, pyramid_s)
{
var slant_height_pyramid =
Math.sqrt(Math.pow(pyramid_s / 2, 2) +
Math.pow(pyramid_h, 2));
document.write( "Slant height of pyramid is: "
+ slant_height_pyramid.toFixed(5));
}
var H1 = 4.5, R = 6;
coneSlantHeight(H1, R);
var H2 = 4, S = 4.8;
pyramidSlantHeight(H2, S);
</script>
|
Output: Slant height of cone is: 7.5
Slant height of pyramid is: 4.66476
Time Complexity: O(sqrt(logH1+logR) + sqrt(logH2+logS))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...