Beginner C# Course
Welcome to our interactive C# course for beginners! This course contains 20 progressive exercises that will teach you C# programming from the ground up. Complete the exercises in order to unlock more advanced topics.
Module 1: Getting Started with C#
Exercise 1: Hello World
Not StartedLet's start with the classic "Hello World" program. This is often the first program written when learning a new language.
Task: Complete the code below by adding the missing line to display "Hello, World!" to the console.
Hint: Look for the TODO comment and use Console.WriteLine() to output text to the console.
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
// TODO: Add a line of code here to display "Hello, World!" to the console
}
}
}
Output:
Code not run yet...
Need a hint?
Replace the TODO comment with this line:
Console.WriteLine("Hello, World!");
Exercise 2: Variables and Output
LockedVariables are used to store data values. In C#, variables must be declared with a specific data type.
Task: Complete the code by filling in the TODOs:
- Create a string variable named
nameand assign your name to it - Create an integer variable named
ageand assign your age (or any number) to it - Complete the Console.WriteLine statement to display your name and age
using System;
namespace VariablesAndOutput
{
class Program
{
static void Main(string[] args)
{
// TODO 1: Create a string variable named 'name' and assign your name to it
// TODO 2: Create an integer variable named 'age' and assign your age to it
// TODO 3: Complete the WriteLine statement to display both variables
Console.WriteLine($"My name is {/* Your code here */} and I am {/* Your code here */} years old.");
}
}
}
Output:
Code not run yet...
Need a hint?
Here's how to complete each TODO:
// TODO 1: Create a string variable named 'name' and assign your name to it
string name = "John";
// TODO 2: Create an integer variable named 'age' and assign your age to it
int age = 25;
// TODO 3: Complete the WriteLine statement to display both variables
Console.WriteLine($"My name is {name} and I am {age} years old.");
Exercise 3: Basic Math Operations
LockedC# supports various arithmetic operations such as addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).
Task: Complete the code by filling in the TODOs to perform basic math operations and display the results.
using System;
namespace MathOperations
{
class Program
{
static void Main(string[] args)
{
// Variables are already declared for you
int a = 10;
int b = 3;
// TODO 1: Calculate the sum of a and b and store in a variable named 'sum'
int sum = // Your code here
// TODO 2: Calculate the difference between a and b (a - b) and store in a variable named 'difference'
int difference = // Your code here
// TODO 3: Calculate the product of a and b and store in a variable named 'product'
int product = // Your code here
// TODO 4: Calculate the quotient of a divided by b and store in a variable named 'quotient'
int quotient = // Your code here
// TODO 5: Calculate the remainder when a is divided by b using the modulus operator and store in a variable named 'remainder'
int remainder = // Your code here
// The code to display the results is already written for you
Console.WriteLine($"a = {a}, b = {b}");
Console.WriteLine($"Sum: {sum}");
Console.WriteLine($"Difference: {difference}");
Console.WriteLine($"Product: {product}");
Console.WriteLine($"Quotient: {quotient}");
Console.WriteLine($"Remainder: {remainder}");
}
}
}
Output:
Code not run yet...
Need a hint?
Here's how to perform each math operation:
// TODO 1: Calculate the sum of a and b and store in a variable named 'sum'
int sum = a + b;
// TODO 2: Calculate the difference between a and b (a - b) and store in a variable named 'difference'
int difference = a - b;
// TODO 3: Calculate the product of a and b and store in a variable named 'product'
int product = a * b;
// TODO 4: Calculate the quotient of a divided by b and store in a variable named 'quotient'
int quotient = a / b;
// TODO 5: Calculate the remainder when a is divided by b using the modulus operator and store in a variable named 'remainder'
int remainder = a % b;
Exercise 4: User Input
LockedPrograms often need to get input from users. In C#, we can use Console.ReadLine() to read user input from the console.
Task: Create a program that:
- Asks the user to enter their name
- Reads the input using
Console.ReadLine() - Asks the user to enter their age
- Reads the age input and converts it to an integer using
int.Parse() - Calculates what year they were born (roughly) by subtracting their age from the current year
- Outputs a personalized message with their name and birth year
using System;
namespace UserInput
{
class Program
{
static void Main(string[] args)
{
// Get the current year (for simplicity, we'll hardcode it)
int currentYear = 2023;
// Ask for user's name
// Read the name input
// Ask for user's age
// Read and convert the age input
// Calculate birth year
// Output personalized message
}
}
}
Output:
Code not run yet...
Need a hint?
Here's how to get user input and convert it:
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.Write("Enter your age: ");
int age = int.Parse(Console.ReadLine());
int birthYear = currentYear - age;
Console.WriteLine($"Hello {name}, you were born around {birthYear}.");