Q:

C# program to calculate the volume of Sphere

belongs to collection: C# Basic Programs | basics

0

C# program to calculate the volume of Sphere

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

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

//C# program to calculate the volume of a Sphere.

using System;

class Sphere
{
    public static float CalculateVolume(float radius)
    {
        float volume = 0.0F;
        
        volume = (float)(4.0 / 3 * Math.PI * radius * radius*radius);

        return volume;
    }
    public static void Main()
    {
        float radius=0.0F;
        float volume = 0.0F;

        Console.Write("Enter the value of radius: ");
        radius = float.Parse(Console.ReadLine());

        volume = CalculateVolume(radius);
        Console.WriteLine("Volume of Sphere: " + volume);
    }
}

Output:

Enter the value of radius: 7
Volume of Sphere: 1436.755
Press any key to continue . . .

Explanation:

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

volume = (float)(4.0 / 3 * Math.PI * radius * radius*radius);

In the Main() method, we created two local variables volume and radius initialized with 0. Then we read the value of the radius from the user after that passed the value of radius into the CalculateVolume() method that will return the calculated volume that will be printed on the console screen.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | basics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to calculate the perimeter of Circle... >>
<< C# program to calculate the area of Sphere...