C# MasterClass

Solved Examples Library

A dedicated example for every lesson in the curriculum.

Ex 1: Printing a Receipt (Output)

Lesson 1: Using Console.WriteLine to print formatted text lines.

Console.WriteLine("--- STORE RECEIPT ---");
Console.WriteLine("Item: Gaming Laptop");
Console.WriteLine("Price: $1200");
Console.WriteLine("Tax:   $120");
Console.WriteLine("---------------------");
Console.WriteLine("Total: $1320");
Output:
--- STORE RECEIPT ---
Item: Gaming Laptop
Price: $1200
Tax:   $120
---------------------
Total: $1320

Ex 2: User Profile (Input)

Lesson 2: Asking the user for their name and year of birth.

Console.Write("Enter your Name: ");
string name = Console.ReadLine();

Console.Write("Enter Birth Year: ");
int year = Convert.ToInt32(Console.ReadLine());
int age = 2025 - year;

Console.WriteLine("Welcome " + name + ", you are " + age + " years old.");
Output:
Enter your Name: Mahmoud
Enter Birth Year: 2000
Welcome Mahmoud, you are 25 years old.

Ex 3: BMI Calculator (Operators)

Lesson 3: Using division and multiplication.

double weight = 70.5;
double height = 1.75;

// Formula: Weight / (Height * Height)
double bmi = weight / (height * height);

Console.WriteLine("Your BMI is: " + bmi);
Output:
Your BMI is: 23.020408...

Ex 4: Grade Checker (Conditions)

Lesson 4: Using if and else if.

int score = 85;

if (score >= 90) {
    Console.WriteLine("Grade: A");
}
else if (score >= 75) {
    Console.WriteLine("Grade: B");
}
else {
    Console.WriteLine("Grade: F");
}
Output:
Grade: B

Ex 5: Factorial Calculator (Loops)

Lesson 5: Using a for loop.

int number = 5;
int result = 1;

for (int i = 1; i <= number; i++) 
{
    result = result * i;
}

Console.WriteLine("Factorial of 5 is: " + result);
Output:
Factorial of 5 is: 120

Ex 6: Find Max Number (Arrays)

Lesson 6: Looping through an array.

int[] scores = { 10, 50, 95, 40, 20 };
int max = scores[0];

for (int i = 1; i < scores.Length; i++) 
{
    if (scores[i] > max) max = scores[i];
}

Console.WriteLine("Highest Score is: " + max);
Output:
Highest Score is: 95

Ex 7: Day of Week (Switch)

Lesson 7: Converting a number to a day name.

int dayCode = 3;

switch (dayCode) 
{
    case 1: Console.WriteLine("Saturday"); break;
    case 2: Console.WriteLine("Sunday"); break;
    case 3: Console.WriteLine("Monday"); break;
    default: Console.WriteLine("Weekend"); break;
}
Output:
Monday

Ex 8: Tax Calculator (Methods)

Lesson 8: Creating a reusable method.

class Program 
{
    static double CalculateTax(double price) 
    {
        return price * 0.10;
    }

    static void Main(string[] args) 
    {
        double tax = CalculateTax(1000.0);
        Console.WriteLine("Tax Amount: $" + tax);
    }
}
Output:
Tax Amount: $100