Performing Arithmetic Operations in C#

Performing Arithmetic Operations in C#

Performing Arithmetic Operations in C#

Arithmetic operations are a fundamental part of programming, and C# provides a wide range of arithmetic operators that you can use to perform basic mathematical calculations. In this article, we'll look at an example of how to perform arithmetic operations in C#.

The Code

The following code demonstrates how to perform arithmetic operations in C#:

	namespace Session1_Basics
	{
	    internal class Program
	    {
	        static void Main(string[] args)
	        {
	            int x;
	            int y;
	            int quotient;
	            int remainder;
	            double result;

	            x = 10;
	            y = 3;
	            quotient = x / y;
	            remainder = x % y;
	            result = (double)x / y;

	            Console.WriteLine("Quotient: " + quotient);
	            Console.WriteLine("Remainder: " + remainder);
	            Console.WriteLine("True Result: " + result);

	            Console.ReadKey();
	        }
	    }
	}
	

Understanding the Code

In the code above, we have defined five variables:

  • x and y: These are integer variables that we will use in our calculations.
  • quotient: This variable will hold the result of dividing x by y.
  • remainder: This variable will hold the remainder of dividing x by y.
  • result: This variable will hold the true result of dividing x by y, including any decimal places.

We have then assigned the values of 10 and 3 to x and y, respectively. We then perform three different arithmetic operations using these variables:

  • quotient = x / y: This calculates the quotient of x divided by y. Since x and y are both integers, the result will also be an integer. In this case, the result will be 3.
  • remainder = x % y: This calculates the remainder of x divided by y. The % operator is the modulus operator, which gives the remainder after integer division. In this case, the remainder will be 1.
  • result = (double)x / y: This calculates the true result of dividing x by y, including any decimal places. We need to cast x as a double before performing the division, since we want the result to include decimal places. In this case, the result will be 3.3333333333333335.

Finally, we use the Console.WriteLine method to output the values of quotient, remainder, and result to the console. We then use the Console.ReadKey method to wait for the user to press a key before exiting the program.

Conclusion

Arithmetic operations are a fundamental part of programming, and C# provides a wide range of arithmetic operators that you can use to perform basic mathematical calculations. In this article, we looked at an example of how to perform arithmetic operations in C#.

If you're interested in learning more about C#, check out our other tutorials and videos on the subject. And if you found this tutorial helpful, please consider subscribing to our YouTube channel!

Video Tutorial

Class Guidelines for Effective 1-on-1 Learning

To keep every session productive and distraction-free, please follow these simple guidelines:

  • Quiet Environment: Join from a calm, private room with minimal background noise. Avoid public or noisy places.
  • No Interruptions: Inform family/roommates in advance. Keep doors closed during class.
  • Mobile on Silent / DND: Set your phone to Silent or Do Not Disturb to prevent calls and notifications.
  • Be Fully Present: Do not multitask. Avoid attending to other calls, visitors, or errands during the session.
  • Stable Setup: Use a laptop/desktop with a stable internet connection and required software installed (Visual Studio/.NET, SQL Server, etc.).
  • Punctuality: Join on time so we can utilize the full session effectively.
  • Prepared Materials (If any): Keep project files, notes, and questions ready for quicker progress.

Following these guidelines helps you focus better and ensures I can deliver the best learning experience in every class.

Schedule a Quick 10-Minute Call

I prefer to start with a short 10-minute free call so I can understand:

  • Your learning objectives and career goals
  • Your current skill level
  • The exact topics you want to learn

Why? Because course content, teaching pace, and fees all depend on your needs — there’s no “one-size-fits-all” pricing. Please leave your details below, and I’ll get back to you to arrange a convenient time for the call.



Google Review Testimonials

.NET Online Training
Average Rating: 4.9
Votes: 50
Reviews: 50