C# Practice Exercises: Essential Techniques for Mastering the Framework

Here’s a short practice session to help you apply what you’ve learned in the C# tutorial:

  1. Create a new C# console application in Visual Studio.
  2. 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();
        }
    }
}
  1. Compile and run your program to make sure it works as expected.
  2. 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, and r 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();
        }
    }
}
  1. 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!

Tags: No tags

Add a Comment

Your email address will not be published. Required fields are marked *