C# Programming Labs

Master C# programming through hands-on coding challenges. Write real code, get instant feedback, and build practical skills for .NET development.

C# Fundamentals - Module 1

Write actual C# code to solve each challenge. Your code is validated for correctness.

Lab 1: Variables & Data Types
Beginner
Coding Challenge
Your Task: Create a complete C# program that stores and displays personal profile information using different data types.

Detailed Requirements:
1. String variable "name" - Store a person's full name (e.g., "John Smith"). In C#, strings are reference types and are immutable. Use double quotes for string literals: string name = "John Smith";. The string keyword is an alias for System.String.

2. Integer variable "age" - Store the person's age as a whole number (e.g., 25). The int type is a 32-bit signed integer ranging from -2,147,483,648 to 2,147,483,647. For larger numbers, use long (64-bit).

3. Double variable "height" - Store height in meters with decimal precision (e.g., 1.75). The double type provides 15-17 digits of precision. For financial calculations, use decimal instead for exact precision.

4. Boolean variable "isStudent" - Store true or false to indicate student status. Note: C# booleans use lowercase (true/false), unlike some other languages.

Output Requirements:
Use Console.WriteLine() to print each variable with a descriptive label. You can use string interpolation (recommended): Console.WriteLine($"Name: {name}"); or concatenation: Console.WriteLine("Name: " + name);

💡 Pro Tips:
• C# uses PascalCase for public members and camelCase for local variables
• Every statement must end with a semicolon ;
• C# is strongly typed - you must declare the type or use var for type inference
• String interpolation with $"" is cleaner than concatenation

Expected Output:
Name: John Smith Age: 25 Height: 1.75 Is Student: True

Requirements Checklist

Declare a string variable named "name" with any value
Declare an int variable named "age"
Declare a double variable named "height"
Declare a bool variable named "isStudent"
Print all variables using Console.WriteLine()
Code compiles without errors
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• String declaration: string name = "John";
• Integer declaration: int age = 25;
• Double declaration: double height = 1.75;
• Boolean declaration: bool isStudent = true;
• Print with interpolation: Console.WriteLine($"Age: {age}");
• Print with concatenation: Console.WriteLine("Age: " + age);
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 2: Conditionals & Logic
Beginner
Coding Challenge
Your Task: Build a grade calculator that converts numeric scores to letter grades using conditional statements (if-else).

Detailed Requirements:
1. Declare an int variable "score" - Initialize with a test value between 0-100. Try different values (95, 82, 71, 65, 45) to test all grade paths.

2. Declare a char variable "grade" - This will store the letter grade ('A', 'B', 'C', 'D', or 'F'). In C#, char uses single quotes: char grade = 'A';. Remember: single quotes for char, double quotes for string.

3. Implement grading logic using if-else chain:
   • if (score >= 90) → Grade A (Excellent - top performers)
   • else if (score >= 80) → Grade B (Good - above average)
   • else if (score >= 70) → Grade C (Average - meets expectations)
   • else if (score >= 60) → Grade D (Below Average - needs improvement)
   • else → Grade F (Failing - below 60)

4. Add pass/fail status: Any score of 60 or above is passing. Use a separate if-else or the ternary operator ?: to determine and print "PASSED" or "FAILED".

Key Concepts:
• The order of conditions matters! Check from highest to lowest. Once a condition is true, remaining else-if blocks are skipped.
• C# comparison operators: >= > < <= == !=
• Ternary operator shorthand: string status = score >= 60 ? "PASSED" : "FAILED";

Expected Output:
Score: 85 Grade: B Status: PASSED

Requirements Checklist

Declare an int variable "score" with a test value
Use if statement to check for grade A (score >= 90)
Include else-if for grades B, C, D
Include else block for grade F
Print pass/fail status using a conditional
Code compiles and runs correctly
Output
// Click "Run Code" to compile and execute
Hints & Tips
• Start with highest grade: if (score >= 90) { grade = 'A'; }
• Chain conditions: else if (score >= 80) { grade = 'B'; }
• Comparison operators: >= > < <= == !=
• Char uses single quotes: 'A' not "A"
• Ternary operator: string status = score >= 60 ? "PASSED" : "FAILED";
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 3: Loops & Iteration
Intermediate
Coding Challenge
Your Task: Implement the famous FizzBuzz challenge - one of the most common programming interview questions that tests your understanding of loops, conditionals, and the modulo operator.

Detailed Requirements:
1. Create a for loop that iterates from 1 to 20 (inclusive).
   Syntax: for (int i = 1; i <= 20; i++) { ... }
   • int i = 1 - initialization (start at 1)
   • i <= 20 - condition (continue while i is 20 or less)
   • i++ - increment (add 1 to i after each iteration)

2. For each number, apply these rules IN THIS EXACT ORDER:
   • If divisible by BOTH 3 AND 5 → print "FizzBuzz"
   • Else if divisible by 3 only → print "Fizz"
   • Else if divisible by 5 only → print "Buzz"
   • Else → print the number itself

Key Concept - Modulo Operator (%):
The % operator returns the remainder after division. If i % 3 == 0, then i divides evenly by 3 (no remainder), meaning i IS divisible by 3.
Examples: 9 % 3 = 0 (divisible), 10 % 3 = 1 (not divisible), 15 % 5 = 0 (divisible)

⚠️ Critical: You MUST check FizzBuzz (both 3 AND 5) FIRST! Why? Numbers like 15 are divisible by both 3 and 5. If you check "Fizz" first, you'll print "Fizz" instead of "FizzBuzz" because the Fizz condition will match before reaching FizzBuzz.

💡 Alternative: C# also supports:
foreach loops for collections
while and do-while loops
• LINQ methods like Enumerable.Range(1, 20)

Expected Output (first 15 numbers):
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz

Requirements Checklist

Create a for loop from 1 to 20
Check FizzBuzz first (divisible by both 3 AND 5)
Check for Fizz (divisible by 3)
Check for Buzz (divisible by 5)
Print the number if none of the above
Output matches expected FizzBuzz pattern
Output
// Expected: 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16, 17, Fizz, 19, Buzz
Hints & Tips
• For loop: for (int i = 1; i <= 20; i++) { }
• Check both: if (i % 3 == 0 && i % 5 == 0)
• Modulo example: 15 % 3 == 0 is true (15 ÷ 3 = 5, no remainder)
• Print number: Console.WriteLine(i);
• Alternative check: i % 15 == 0 for FizzBuzz (15 = 3 × 5)
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below