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

Any Query / Enrollment Request



Google Review Testimonials

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