Click here to Skip to main content
14,669,432 members
Rate this:
Please Sign up or sign in to vote.
See more:
The first picture is what my professor wants us to do:
https://prnt.sc/v1drxs

Here is MY code:
Console.Write("Enter the radius for a circle: ");

double radius = Convert.ToDouble(Console.ReadLine());
double diameter = 2 * radius;
double circumference = Convert.ToDouble(Math.PI * diameter);

Console.WriteLine("\nThe radius of the circle is " + radius + " units.");
Console.WriteLine("The diameter of the circle is " + diameter + " units.");
Console.WriteLine("The circumference of the circle is " + circumference + " units.");

Console.Write("\nPress any key to continue...");

Console.ReadKey();


What I have tried:

I am using Microsoft Visual Studio 2017. I'm very new to coding. Help would be appreciated, thanks!
Posted
Updated 18-Oct-20 18:01pm
v2

Rate this:
Please Sign up or sign in to vote.

Solution 1

Try using formatting:
Console.WriteLine("\nThe radius of the circle is {0:N2} units.",  radius);
Or (if 2017 supports interpolation and I can't remember if it does)
Console.WriteLine($"\nThe radius of the circle is {radius:N2} units.");
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Since C# 6.0, you can do string interpolation and would be easiest for you to do such print output.

Example:
string name = "Mark";
var date = DateTime.Now;

// Composite formatting:
Console.WriteLine("Hello, {0}! Today is {1}, it's {2:HH:mm} now.", name, date.DayOfWeek, date);
// String interpolation:
Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");
// Both calls produce the same output that is similar to:
// Hello, Mark! Today is Wednesday, it's 19:40 now.

Reference for you to learn:
$ - string interpolation - C# reference | Microsoft Docs[^]
Strings.Format(Object, String) Method (Microsoft.VisualBasic) | Microsoft Docs[^]

Your case, you can try something like:
Console.WriteLine($"\nThe diameter of the circle is {diameter:F2} units.");
Console.WriteLine($"\nThe circumference of the circle is {circumference:F2} units.");
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100