Volume of pyramid

The volume of the pyramid when the base is rectangular.

The volume of the pyramid when the base is square.

Formula: volume=1/3*(area of base)*height 

Example 1:

Input: length=5,width=4,height=12
Output: 80

Approach 1: Volume of the pyramid when the base is rectangular

Java

public class VolumeofPyramid {
    public static void main(String[] args) {
        double length = 5;
        double width = 4;
        double height = 12;
        double volume = volumeOfPyramid(length, width, height);
        System.out.println("Volume is " + volume);
    }

///function to find the volume
//of rectangular base pyramid
    private static double volumeOfPyramid(double length
            double widthdouble height) {
        // formula =1/3*areaofbase*height
        double volume = (length * width * height) / 3;
        return volume;
    }
}

C++

#include <bits/stdc++.h>
using namespace std;

//function to find the volume
//of rectangular base pyramid
double volumeOfPyramid(double length,double width,double height)
{
    //formula =1/3*areaofbase*height
    double volume=(length*width*height)/3;
    return volume;
}
int main()
{
    double length=5;
    double width=4;
    double height=12;
    double volume=volumeOfPyramid(length,width,height);
    cout<<"Volume is ";
    cout<<volume<<"\n";
    return 0;
}

Approach 2: Volume of the pyramid when the base is square

Java

public class VolumeofPyramid {
    public static void main(String[] args) {
        double length = 5;
        double height = 12;
        double volume = volumeOfPyramid(length, height);
        System.out.println("Volume is " + volume);
    }

///function to find the volume
//of square  base pyramid
    private static double volumeOfPyramid(double lengthdouble height) {
        // formula =1/3*areaofbase*height
        double volume = (length * length * height) / 3;
        return volume;
    }
}

C++

#include <bits/stdc++.h>
using namespace std;

//function to find the volume
//of square base pyramid
double volumeOfPyramid(double length,double height)
{
    //formula =1/3*areaofbase*height
    double volume=(length*length*height)/3;
    return volume;
}
int main()
{
    double length=5;
    double height=12;
    double volume=volumeOfPyramid(length,height);
    cout<<"Volume is ";
    cout<<volume<<"\n";
    return 0;
}



No comments:

Post a Comment