C# program to calculate the volume of a cone

Here, we are going to learn how to calculate the volume of a cone in C#?
Submitted by Nidhi, on October 11, 2020

Here we will calculate the volume of the Cone using the below formula.

volume = (1.0 / 3) * Math.PI * radius * radius * height;

Program:

The source code to calculate the volume of a Cone is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to calculate the value of a cone.

using System;

class Cone
{
    public double CalculateVolume(double radius, double height)
    {
        double volume = 0.0;

        volume = (1.0 / 3) * Math.PI * radius * radius * height;

        return volume;
    }
    public static void Main()
    {
        double volume = 0;
        double radius   =   0;
        double height   =   0;

        Cone C = new Cone();

        Console.Write("Enter the radius of a cone: ");
        radius = double.Parse(Console.ReadLine());

        Console.Write("Enter the height of a cone: ");
        height = double.Parse(Console.ReadLine());

        volume = C.CalculateVolume(radius, height);

        Console.WriteLine("Volume of cone is: "+ volume);
    }
}

Output:

Enter the radius of a cone: 10
Enter the height of a cone: 2.4
Volume of cone is: 251.327412287183
Press any key to continue . . .

Explanation:

Here, we created a class Cone that contains two methods CalculateVolume() and Main(). The CalculateVolume() method is used to calculate the volume of Cone using the below formula and return the calculated area to the calling method.

volume = (1.0 / 3) * Math.PI * radius * radius * height;

In the Main() method, we created three local variables volume, radius, and height initialized with 0. Then we created the object of Cone class and read the value of radius and height, after that passed the value of radius and height into the CalculateVolume() method that will return the calculated volume of the cone that will be printed on the console screen.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.