C#: Calculate and display the surface and volume of a sphere
Write a C# Sharp program that takes the radius of a sphere as input and calculates and displays the surface and volume of the sphere.
C# Sharp: Area and volume of a sphere
A sphere is a perfectly round geometrical object in three-dimensional space that is the surface of a completely round ball.
In three dimensions, the volume inside a sphere is derived to be V = 4/3*π*r3 where r is the radius of the sphere
![C# Sharp: Volume of a sphere](https://www.w3resource.com/w3r_images/csharp-volume-of-sphere-data-types-exercise-8.png)
The area of a sphere is A = 4*π*r2
![C# Sharp: Area of a sphere](https://www.w3resource.com/w3r_images/csharp-area-of-sphere-data-types-exercise-8.png)
Sample Solution:-
C# Sharp Code:
using System;
public class Exercise8
{
public static void Main()
{
// Declare variable to store the radius of the sphere
float r;
// Define the value of pi
float pi = 3.1415926535f;
// Input the radius of the sphere
Console.Write("Radius: ");
r = Convert.ToSingle(Console.ReadLine());
// Calculate and display the surface area of the sphere
Console.WriteLine("Surface area of the sphere: " + 4 * pi * (r * r));
// Calculate and display the volume of the sphere
Console.WriteLine("Volume of the sphere: " + 4f / 3f * pi * (r * r * r));
}
}
Sample Output:
Radius: 2 50.26548 33.51032
Flowchart:
![Flowchart: Calculate the speed in kilometers and miles per hour from distance and time.](https://www.w3resource.com/w3r_images/csharp-datatype-exercise-flowchart-8.png)
C# Sharp Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a C# Sharp program that takes distance and time as input and displays the speed in kilometers per hour and miles per hour.
Next: Write a C# Sharp program that takes a character as input and check the input (lowercase) is a vowel, a digit, or any other symbol.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics