Use the C Sharp mathematical operator

The author James Payne,

August 28, 2021

[] (api.whatsapp.com/send?text=W… www.codeguru.com/csharp/c-sh… ] (reddit.com/submit?url=…).

We discussed C# data types in the last article, and we’ll follow up today with a.net tutorial that shows developers how to work with one of the data types we’ve covered: digital data types. In particular, we’ll learn how to use C# mathematical operators and perform mathematical operations using C#. We’ll provide plenty of code examples for you to practice with, so fire up your favorite integrated Development environment (IDE) or code editor and let’s get started!

If you missed the previous articles in this series, we encourage you to read them. Again, if you need to recall, you can always revisit it via the following link:

  • C# data type explanation
  • Comment on best practices in C#
  • C # beginners
  • Use strings in C#

Mathematical operators in C#

Below you’ll find code examples that show how to use various math and number operators in C#. Some of them work in a similar way to the math operators you’re used to in school and everyday life. We’ll start with one of the most common operators in all of C# — the assignment operator

Assignment operator in C#

No doubt you have seen assignment operators many times in your life; It is written with an equal sign or *=. There are several assignment operators available in C#, the most basic being to assign a simple value to a variable. The item to the left of the = sign is the variable name, and the value to the right of the =* sign is the value you assign to the variable. Here’s how to use the basic assignment operator in C# code:

using System;
 
public class Program
{
            	public static void Main()
            	{
                            	int age = 44;
                                            	
                            	Console.WriteLine("Age is equal to: {0}", age); }}Copy the code

In this code, we create an integer variable named age and assign it a value of 44. Int age = 44; Is implemented in the line. Next, we use the print statement console. WriteLine to print some text and variable values to the screen. Running this code in your code editor produces the following output:

Age is equal to 44
Copy the code

You can also use other variables to assign values to variables using the assignment operator, as shown in the following example:

using System;
 
public class Program
{
            	public static void Main()
            	{
                            	int age = 44;
                            	int ageTwo = 44;
                            	int futureAge = age + ageTwo;	
                            	Console.WriteLine("Age is equal to: {0}", age);
                            	Console.WriteLine("Your age in another 44 years will be: {0}", futureAge); }}Copy the code

In this example, we created three variables. The first variable age is my age. The second variable, ageTwo, also represents my age. Finally, we create a third variable, futureAge, to which we assign the value of age + ageTwo — indicating that you can use the assignment operator to assign the value of one or more variables to another. The result of running this code is:

Age is equal to: 44
Your age in another 44 years will be: 88
Copy the code

Another way to assign the value of an existing variable to a new variable is simply to do the following:

using System; public class Program { public static void Main() { int age = 44; int ageTwo = age; Console.WriteLine("Age is equal to: {0}", age); Console.WriteLine("ageTwo is equal to: {0}", ageTwo); }}Copy the code

Results:

Age is equal to: 44
ageTwo is equal to: 44
Copy the code

C# addition + operator

You have already used the addition operator in your life or in math. When you apply it to integer values or floating point numbers, you can use it to add two or more numbers. Note that there is a difference between adding an int and adding a float. We’ll talk about that difference later. First, take a look at the following example, which demonstrates how to use the C# math *+ operator * :

using System;
 
public class Program
{
            	public static void Main()
            	{
                            	int valueOne = 12;
                            	int valueTwo = 8;
                            	
                            	Console.WriteLine("valueOne added to valueTwo is equal to: {0}", valueOne+valueTwo); }}Copy the code

This code adds the values of two integers, resulting in:

valueOne added to valueTwo is equal to: 20
Copy the code

Another use of the *+ operator * for int in C# is to use it to assign values to variables. See the following code example:

using System;
 
public class Program
{
            	public static void Main()
            	{
                            	int valueOne = 8+12;
                                                            	
                            	Console.WriteLine("The value of valueOne is equal to: {0}", valueOne); }}Copy the code

Here, we create a variable named valueOne and assign it to the result of the equation 8+12. Running it in the IDE results in:

The value of valueOne is equal to: 20
Copy the code

Any time you add an integer value to another integer, the result is always an integer value. However, if you add an integer to a floating point number, the result is always a floating point number. Run the following code to see what happens:

using System;
 
public class Program
{
            	public static void Main()
            	{
                            	int valueInteger = 8;
                            	float f2 = 1.02 f;              	
                            	Console.WriteLine("The valueInteger plus f2 equals: {0}", valueInteger + f2); }}Copy the code

Run this code to output:

The valueInteger plus f2 equals: 9.02
 
Copy the code

One final thing to note: When you add two floating point numbers, as you might expect, it is always a floating point number.

Subtraction operator in C#

The subtraction operator in C# works the same way as subtracting from a mathematical equation. As with the *+ operator *, when you subtract an integer from an integer, you always return an integer value. However, when you subtract an integer from a floating point number (and vice versa), the value is always a floating point number.

Here’s how to subtract two integer values in C# using the * – operator * :

using System;
 
public class Program
{
            	public static void Main()
            	{
                            	int valueInteger1 = 8;
                            	int valueInteger2 = 12;               	
                            	Console.WriteLine("valueInteger2 minus valueInteger1 equals: {0}", valueInteger2 - valueInteger1); }}Copy the code

The output of this code is:

valueInteger2 minus valueInteger1 equals: 4
Copy the code

Here is some code that shows what happens when you subtract an integer from a floating-point number and vice versa:

using System;
 
public class Program
{
            	public static void Main()
            	{
                            	int valueInteger1 = 8;
                            	float floatValue = 1.02 f;             	
                            	Console.WriteLine("valueInteger1 minus floatValue equals: {0}", valueInteger1 - floatValue); }}Copy the code

Here, the result of running this code is:

ValueInteger1 minus floatValue equals: 6.98Copy the code

Finally, you can assign a value to a variable by subtracting the two variables and storing the result of the equation in the variable. Here’s the code:

using System;
 
public class Program
{
            	public static void Main()
            	{
                            	int valueInteger1 = 8;
                            	int valueInteger2 = 12;               	
                            	int valueInteger3 = valueInteger2 - valueInteger1;
                            	Console.WriteLine("valueInteger3 is equal to: {0}", valueInteger3); }}Copy the code

This code adds the result of equations 12 — 8 — or valueInteger2 — valueInteger1 to a variable named valueInteger3 *. What are the results of running this code in your IDE? *

valueInteger3 is equal to: 4
Copy the code

The multiplication * operator in C#

Multiplying two or more values, like its mathematical equations, you may have used X multiplications in school, such as 4×2 = 8. When you multiply two integers, you always get an integer. However, when you multiply an integer by a floating point, the result is always a floating point. Also, multiplying two or more decimals is always the result of a decimal number.

Here is an example of how to use the multiplication * operator on two integer values in C# :

using System;
 
public class Program
{
            	public static void Main()
            	{
                            	int valueInteger1 = 8;
                            	int valueInteger2 = 12;               	
                            	
                            	Console.WriteLine("valueInteger1 times valueInteger2 is equal to: {0}", valueInteger1 * valueInteger2); }}Copy the code

Here is the result of running this code:

valueInteger1 times valueInteger2 is equal to: 96
Copy the code

Again, here’s some code that shows what happens when you multiply an integer by a floating point number in C#

using System;
 
public class Program
{
            	public static void Main()
            	{
                            	int valueInteger1 = 8;
                            	float valueFloat = 1.2 f;               	
                            	
                            	Console.WriteLine("valueInteger1 times valueFloat is equal to: {0}", valueInteger1 * valueFloat); }}Copy the code

The result of this code snippet is:

ValueInteger1 times valueFloat is equal to: 9.6Copy the code

Finally, as with the *+ and – operators *, you can of course assign to the variable by multiplying the result of the two variables. Here’s an example from the code:

using System;
 
public class Program
{
            	public static void Main()
            	{
                            	int valueInteger1 = 8;
                            	int valueInteger2 = 4;                  	
                            	int valueInteger3 = valueInteger1 * valueInteger2;
                            	Console.WriteLine("valueInteger3 is equal to: {0}", valueInteger3); }}Copy the code

The result of running this code will give you:

valueInteger3 is equal to: 32
Copy the code

C# division/operator

The/operation is used to separate two or more numeric values. When using the division operator, note that dividing an integer by an integer always yields an integer — even if there is remainder. If a remainder occurs, C# rounds down. Consider the following code:

using System;
 
public class Program
{
            	public static void Main()
            	{
                            	int valueInteger1 = 7;
                            	int valueInteger2 = 4;                  	
                            	int valueInteger3 = valueInteger1 / valueInteger2;
                            	Console.WriteLine("valueInteger3 is equal to: {0}", valueInteger3); }}Copy the code

Normally, if you divide by 7/4, you would expect to get a result of 1 with a remainder of 3. However, since we are dividing two integers, C# can only return one integer, so you get the following result:

valueInteger3 is equal to: 1
Copy the code

If you divide an integer by a floating point number, you always get a floating point number:

using System;

public class Program
{
public static void Main()
{
int valueInteger1 = 7;
float valueFloat = 4.0 f;

Console.WriteLine("valueInteger1 / valueFloat is equal to: {0}", valueInteger1 / valueFloat); }}Copy the code

The result of this code is:

ValueInteger1 / valueFloat is equal to: 1.75Copy the code

Finally, you can assign a value to a variable by dividing two numbers and assigning their values to it:

using System;
 
public class Program
{
            	public static void Main()
            	{
                            	int valueInteger1 = 7;
                            	int valueInteger2 = 4;                  	
                            	int valueInteger3 = valueInteger1 / valueInteger2;
                            	Console.WriteLine("valueInteger3 is equal to: {0}", valueInteger3); }}Copy the code

The following output is displayed:

valueInteger3 is equal to: 1
Copy the code

The modular % operator in C#

If you really want the remainder after division, C# has a built-in mathematical operator for such functions: the modulo operator is the *% operator *. Note that using this mathematical operator returns only the remainder of the division. Here’s how to use modular operators in C# :

using System;
 
public class Program
{
            	public static void Main()
            	{
                            	int valueInteger1 = 7;
                            	int valueInteger2 = 4;                  	
                            	
                            	Console.WriteLine("The remainder of valueInteger1 / valueInteger2 is: {0}", valueInteger1 % valueInteger2); }}Copy the code

Since 7/4 is equal to 1 and has a remainder of 3, using modulo results in the output:

The remainder of valueInteger1 / valueInteger2 is: 3
Copy the code

Remember – only the remainder is returned when using modular operators.

Of course, we can also create a variable using a module:

using System;
 
public class Program
{
            	public static void Main()
            	{
                            	int valueInteger1 = 7;
                            	int valueInteger2 = 4;                  	
                            	int valueInteger3 = valueInteger1 % valueInteger2;
                            	Console.WriteLine("The value of valueInteger3 is: {0}", valueInteger3); }}Copy the code

The result of this code is:

The value of valueInteger3 is: 3
Copy the code

Other assignment operators in C#

In addition to the basic or normal assignment operators we discussed at the beginning of this tutorial, C# provides a number of other assignment operators. We can’t cover all of this in this article, so we’ll show you the rest in another article. But we can show you some here and give you a sneak peek. Note that not all assignment operators are mathematical in nature. Some are bitwise and shift operators.

The remaining C# assignment operators include:

  • = operator The general assignment operator.
  • The += addition assignment operator
  • -= Subtraction assignment operator
  • *= Multiplicative assignment operator
  • /= The division assignment operator
  • %= the modular assignment operator

Run the following code to see each of these C# assignment operators:

using System;
 
public class Program
{
            	public static void Main()
            	{
                            	int additionAssignmentExample = 0;
                            	int subtractionAssignmentExample = 0;
                            	int multiplicationAssignmentExample = 1;
                            	int divisionAssignmentExample = 1;
                            	int moduloAssignmentExample = 1;
                            	
                            	Console.WriteLine(additionAssignmentExample +=5);
                            	Console.WriteLine(subtractionAssignmentExample -=5);
                            	Console.WriteLine(multiplicationAssignmentExample *=5);
                            	Console.WriteLine(divisionAssignmentExample /=5);
                            	Console.WriteLine(moduloAssignmentExample %=5); }}Copy the code

The result of this code is:

5 minus 5, 5, 0, 1Copy the code

In this example, the += operator basically says x = x +5. For example, we first is additionalAssignmentExample distribution value of 0. Then, we use the * + = assignment operator *, its value increased allocation additionAssignmentExample itself, plus add 5 to it.

The rest of the assignment operators work in a similar way, except that they subtract, multiply, divide, or return the modulus * of the number to the right of the *= symbol.