Here’s a short practice session to help you apply what you’ve learned in the C# tutorial:
- Create a new C# console application in Visual Studio.
- Write a program that asks the user for their name and then greets them with a message that includes their name. Here’s an example of what the program might look like:
csharpCopy codeusing System;
namespace GreetingProgram
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter your name: ");
string name = Console.ReadLine();
Console.WriteLine("Hello, " + name + "!");
Console.ReadLine();
}
}
}
- Compile and run your program to make sure it works as expected.
- Write a program that calculates the area of a circle with a given radius. The program should ask the user to enter the radius, and then print the area of the circle. The formula for the area of a circle is:
A = π * r^2
, whereπ
is approximately equal to 3.14, andr
is the radius. Here’s an example of what the program might look like:
csharpCopy codeusing System;
namespace CircleArea
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter the radius of the circle: ");
double radius = double.Parse(Console.ReadLine());
double area = Math.PI * Math.Pow(radius, 2);
Console.WriteLine("The area of the circle is: " + area);
Console.ReadLine();
}
}
}
- Compile and run your program to make sure it works as expected.
These exercises should give you a good idea of how to write simple programs in C# and help you practice using the language. Have fun!