Sphere, Float - C# Programming Exercise

In this C# exercise, you are required to write a program that calculates the surface area and volume of a sphere, given its radius. The formulas provided are as follows: the surface area of a sphere is calculated using the formula 4 * pi * radius squared, and the volume is calculated with the formula 4/3 * pi * radius cubed. Since this exercise involves calculations with decimal numbers, it is crucial to use Convert.ToSingle(...) to convert the user's inputs to floating-point numbers, ensuring that the calculations are performed accurately.

This exercise will help you understand how to perform advanced mathematical calculations in C# and how to handle floating-point data types. You will also become familiar with using the constant pi in mathematical formulas and applying geometric formulas in programming. It is an excellent exercise to learn about manipulating floating-point numbers and applying mathematical principles in software development using C#.

Moreover, such exercises have practical applications in fields like scientific simulations, 3D modeling, and even in creating applications for engineering and architecture.

 Category

Basic Data Types

 Exercise

Sphere, Float

 Objective

Write a C# program that calculate the surface area and volume of a sphere, given its radius (surface area = 4 * pi * radius squared; volume = 4/3 * pi * radius cubed).

Note: For floating-point numbers, you should use Convert.ToSingle(...)

 Write Your C# Exercise

using System;  // Import the System namespace to use basic classes like Console

class Program  // Define the main class of the program
{
    static void Main()  // The entry point of the program
    {
        // Ask the user for the radius of the sphere
        Console.Write("Enter the radius of the sphere: ");  // Display prompt for radius
        float radius = Convert.ToSingle(Console.ReadLine());  // Read and convert the user's input for radius to a single precision float

        // Calculate the surface area of the sphere: 4 * pi * radius^2
        float surfaceArea = 4 * (float)Math.PI * (radius * radius);  // Use Math.PI for the value of pi

        // Calculate the volume of the sphere: (4/3) * pi * radius^3
        float volume = (4f / 3f) * (float)Math.PI * (radius * radius * radius);  // Volume formula using Math.PI for pi

        // Display the calculated surface area and volume
        Console.WriteLine($"Surface Area of the sphere: {surfaceArea:F2} square units");  // Display the surface area, formatted to 2 decimal places
        Console.WriteLine($"Volume of the sphere: {volume:F2} cubic units");  // Display the volume, formatted to 2 decimal places
    }
}

 Share this C# exercise

 More C# Programming Exercises of Basic Data Types

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Vowel - switch

    In this C# exercise, you are asked to write a program that prompts the user for a symbol and, using the switch statement, determines whether the symbol entered is a ...

  •  Vowel - if

    In this C# exercise, you are asked to write a program that prompts the user for a symbol and determines if it is a vowel (in lowercase), a digit, or any other ...

  •  Triangle, NorthEast

    In this C# exercise, you are asked to write a program that prompts the user for a width and then displays a triangle where the base has the number of asterisks corresponding...

  •  Prime factors

    In this C# exercise, you are asked to write a program that takes a number entered by the user and displays it as a product of its prime factors. Prime factors are numbers th...

  •  If, symbols

    In this C# exercise, you are asked to write a program that prompts the user for a symbol and determines if it is an uppercase vowel, a lowercase vowel, a digit...

  •  Char + for

    In this C# exercise, you are asked to write a program that prints the letters from B to N in uppercase, using a for loop. This type of exercise is perfect for ...