C# Conditional Statements Coding Exercises
Hey there, future C# pro! 🚀 If you’re just getting started with conditional statements, you’re in the right place. In this lesson, we’ll put your skills to the test with some easy coding exercises. You’ll get hands-on practice with if-else
, switch-case
, and a few advanced conditions.
No worries—I’ll guide you with hints, but the solutions are all yours to figure out! Ready? Let’s dive in!
Â
Exercise 1: Grading System (if-else)
Problem:
Write a C# program that takes a student’s score (0-100) as input and prints their grade based on the following rules:
- 90-100 → A
- 80-89 → B
- 70-79 → C
- 60-69 → D
- Below 60 → F
Hint:
Use if-else
statements to check the range of the score and print the corresponding grade.
Output:
Enter your score: 85
Your grade is: B
 Exercise 2: Odd or Even (if-else)
Problem:
Create a program that asks the user for a number and determines whether it’s odd or even.
Hint:
Use the modulus operator %
to check if a number is divisible by 2.
Output 1:
Enter a number: 7
7 is an odd number.
Output 2:
Enter a number: 10
10 is an even number.
 Exercise 3: Simple Calculator (switch-case)
Problem:
Write a basic calculator that takes two numbers and an operator (+
, -
, *
, /
) as input, then performs the corresponding operation and displays the result.
Hint:
Use a switch-case
statement to handle different operators. Don’t forget to handle division by zero!
Output:
Enter first number: 10
Enter operator (+, -, *, /): *
Enter second number: 5
Result: 10 * 5 = 50
 Exercise 4: Day of the Week (switch-case)
Problem:
Write a program that takes a number (1-7) as input and prints the corresponding day of the week. For example, input 1
should output Monday
, 2
should output Tuesday
, and so on.
Hint:
Use switch-case
to match numbers to days. Handle invalid inputs too!
Output:
Enter a number (1-7): 3
Wednesday
 Exercise 5: Leap Year Checker (Advanced Conditionals)
Problem:
Create a program that asks the user for a year and checks if it’s a leap year. A leap year:
- Is divisible by 4
- But not divisible by 100 unless it is also divisible by 400
Hint:
Use a combination of if-else
statements and logical operators (&&
, ||
) to check the conditions.
Output 1:
Enter a year: 2024
2024 is a leap year.
Output 2:
Enter a year: 2023
2023 is not a leap year.
 Exercise 6: Login System (Advanced Conditionals)
Problem:
Build a simple login system where the user enters a username and password. If the username is "admin"
and the password is "pass123"
, display “Login successful.” Otherwise, show “Invalid credentials.”
Hint:
Use an if-else
condition to compare the user’s input with stored credentials.
Output 1:
Enter username: admin
Enter password: pass123
Login successful.
Output 2:
Enter username: user
Enter password: wrongpass
Invalid credentials.
Conclusion
And that’s a wrap! 🎉 You just tackled some fun coding exercises on conditional statements in C#. These are essential for making real-world applications, from simple decision-making to handling multiple conditions.
Want a challenge? Try modifying these programs with extra features, like handling invalid inputs or adding more conditions!Â
🚀 Next What?
Hey, congrats! 🎉 You’ve just wrapped up the chapter on conditional statements—including if-else, switch-case, and other decision-making structures. That’s a big step forward! 🙌 These tools are super useful when you need your program to make choices.
But wait, what if you need to repeat a task multiple times without writing the same code again and again? 🤔 That’s where loops come in! 🌀
👉 In the next chapter, you’ll dive into C# Loops, where you’ll learn how to:
✅ Repeat actions efficiently 💪
✅ Use different types of loops like for
, while
, and foreach
🔄
✅ Make your programs smarter and more efficient 🚀
Keep coding, and see you in the next lesson! 🚀