2.6. Studio: Area of a Circle¶
To get started, create a new console application in Visual Studio for the studio.
2.6.1. Calculate the Area of a Circle¶
Write a program that prompts the user for the radius of a circle. Calculate the area of the user’s circle and print the result.
Recall that the area of a circle is
A = pi * r * r where
r is the radius.
Here’s an example of how your program should work:
Enter a radius: 2.5 The area of a circle of radius 2.5 is: 19.625
Some questions to ask yourself:
What data type should the radius be?
What is the best way to get user input into a variable
radiusof that type?
System.Math class in C# to get the value of pi and square the radius.
The documentation has guidance on how to use the
PI field and the
2.6.2. More Calculations¶
Using the same radius, calculate the circumference (
2*pi*r) and diameter of the circle (
Output the results.
2.6.3. Road Trip!¶
Ask the user for the miles per gallon of their car.
If the radius that they entered is in miles, output how many gallons of gas they will use to go around this circle.
2.6.4. Bonus Missions¶
Think about how we could make this program more modular by breaking out some of the code into a separate class. For example, we could pull out the circle information into a
Circleclass and leave the user questions and console messages in
Program. Take a look at the using statement for a refresher on how to reference another class file.
Extend your program further by using a while or do-while loop, so that when the user enters a negative number they are re-prompted for a radius.
Add additional validation to your program. If the user enters a non-numeric character or a empty string? Print an error message and quit. You’ll need to peek ahead to learn about conditional syntax in C#.