Complete C# Tutorial

C# Operator Programming Examples | Practice with Solutions

Before we dive into practice problems, let’s take a step back and see why operators are such a big deal in C#. You’ve already seen variables, data types, and how to store values. But what’s next? Well, we need a way to do something with those values—like adding numbers, comparing values, or making decisions. That’s where operators come in!

Think of them as tools that help you work with data. Whether you’re crunching numbers, making logical decisions, or updating values, operators make coding way easier (and fun too!). 😃

So, in this lesson, we’ll get hands-on with some simple and fun exercises. Each exercise comes with an easy explanation, hints, and expected output. By the end of this, you’ll be using these operators like a champ! 💪

Let’s get started! 🎉

1️⃣ Arithmetic Operators – Simple Calculator

Question:

Write a program that takes two numbers as input and performs basic arithmetic operations: addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).

Hint:

  • Use +, -, *, /, and % to perform operations.
  • Store the results in separate variables.

Solution:

				
					using System;

class ArithmeticExample
{
    static void Main()
    {
        int num1 = 10, num2 = 3;

        Console.WriteLine("Addition: " + (num1 + num2));
        Console.WriteLine("Subtraction: " + (num1 - num2));
        Console.WriteLine("Multiplication: " + (num1 * num2));
        Console.WriteLine("Division: " + (num1 / num2));
        Console.WriteLine("Modulus: " + (num1 % num2));
    }
}
				
			

Output:

				
					Addition: 13  
Subtraction: 7  
Multiplication: 30  
Division: 3  
Modulus: 1  				
			

💡 Try changing num1 and num2 to see different results!

2️⃣ Comparison Operators – Who is Older?

Question:

Write a program that compares the ages of two people and tells who is older, younger, or if they are the same age.

Hint:

  • Use >, <, and == to compare ages.

Solution:

				
					using System;

class ComparisonExample
{
    static void Main()
    {
        int age1 = 25, age2 = 30;

        if (age1 > age2)
            Console.WriteLine("Person 1 is older.");
        else if (age1 < age2)
            Console.WriteLine("Person 2 is older.");
        else
            Console.WriteLine("Both are the same age.");
    }
}
				
			

Output:

				
					Person 2 is older.
				
			

🎯 Try changing age1 and age2 to test different scenarios!

3️⃣ Logical Operators – Can You Enter the Club?

Question:

A club allows entry only if you are 18 or older and have a VIP pass. Write a program that checks if a person can enter.

Hint:

  • Use && (AND) for both conditions.
  • Use || (OR) to check for alternative conditions.

Solution:

				
					using System;

class LogicalExample
{
    static void Main()
    {
        int age = 20;
        bool hasVipPass = true;

        if (age >= 18 && hasVipPass)
            Console.WriteLine("Welcome to the club! 🎉");
        else
            Console.WriteLine("Sorry, you can't enter. 😢");
    }
}
				
			

Output:

				
					Welcome to the club! 🎉				
			

🔥 Try changing age and hasVipPass to see when entry is allowed or denied!

4️⃣ Increment & Decrement Operators – Counting Game

Question:

Write a program that counts from 1 to 5 using increment operators and then counts backward using decrement operators.

Hint:

  • Use ++ to increase the number.
  • Use -- to decrease the number.

Solution:

				
					using System;

class IncrementDecrementExample
{
    static void Main()
    {
        int count = 1;

        Console.WriteLine("Counting up:");
        Console.WriteLine(count++); // 1
        Console.WriteLine(count++); // 2
        Console.WriteLine(count++); // 3
        Console.WriteLine(count++); // 4
        Console.WriteLine(count++); // 5

        Console.WriteLine("Counting down:");
        Console.WriteLine(count--); // 5
        Console.WriteLine(count--); // 4
        Console.WriteLine(count--); // 3
        Console.WriteLine(count--); // 2
        Console.WriteLine(count--); // 1
    }
}
				
			

Output:

				
					Counting up:  
1  
2  
3  
4  
5  

Counting down:  
5  
4  
3  
2  
1  
				
			

🔄 Experiment with ++count and --count to see the difference!

5️⃣ Assignment Operators – Bank Balance Update

Question:

You deposited some money in the bank and later withdrew some. Write a program to update your balance using assignment operators.

Hint:

  • Use += to add money.
  • Use -= to withdraw money.

Solution:

				
					using System;

class AssignmentExample
{
    static void Main()
    {
        double balance = 1000;

        balance += 500; // Deposit $500
        Console.WriteLine("After deposit: $" + balance);

        balance -= 200; // Withdraw $200
        Console.WriteLine("After withdrawal: $" + balance);
    }
}
				
			

Output:

				
					After deposit: $1500  
After withdrawal: $1300  
				
			

💰 Try different deposit and withdrawal amounts!

6️⃣ Conditional Operator – Even or Odd?

Question:

Write a program that checks if a number is even or odd using the conditional operator (?:).

Hint:

  • Use % 2 == 0 to check for even numbers.
  • Use the ?: operator for quick decisions.

Solution:

				
					using System;

class ConditionalExample
{
    static void Main()
    {
        int number = 7;

        string result = (number % 2 == 0) ? "Even" : "Odd";
        Console.WriteLine("The number is: " + result);
    }
}
				
			

Output:

				
					The number is: Odd				
			

🔢 Try changing the number to test different values!

Bonus: Use All Operators in One Program! 🎁

Here’s a challenge: Can you create a program that uses all these operators together? Try something fun, like a simple banking system or a basic calculator with conditions! 🚀

What’s Next? Get Ready to Code! 💻

Alright, you’ve learned about all these cool operators. But knowing about them isn’t enough—you’ve got to use them! 😃

In the next lesson, you’ll get some fun programming exercises where you’ll have to apply everything you’ve learned so far. Yep, this time, you’ll be the one writing the code! ✍️💡

Don’t worry—I’ll give you clear instructions and hints to help you out. Plus, you’ll see the expected output so you can check if your solution is on the right track. It’s going to be a great way to test your skills and boost your confidence. 🚀

So, are you ready? Let’s put your coding skills to the test in the next lesson! See you there! 🎉

Leave a Comment

Share this Doc

Programming examples

Or copy link