C# program to calculate the area of Sphere

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

Here, we will calculate the area of Sphere using the below formula.

area = (float)(4 * Math.PI * radius * radius);

Program:

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

//C# program to calculate the area of Sphere.

using System;

class Sphere
{
    public static float CalculateArea(float radius)
    {
        float area = 0.0F;
        
        area = (float)(4 * Math.PI * radius * radius);

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

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

        area = CalculateArea(radius);
        Console.WriteLine("Area of Sphere: "+area);
    }
}

Output:

Enter the value of radius: 8
Area of Sphere: 804.2477
Press any key to continue . . .

Explanation:

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

area = (float)(4 * Math.PI * radius * radius);

In the Main() method, we created two local variables area and radius initialized with 0. Then we read the value of the radius from the user, after that passed the value of radius into CalculateArea() method that will return the calculated area 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.