0
0
C Sharp (C#)programming~5 mins

Arithmetic operators in C Sharp (C#)

Choose your learning style9 modes available
Introduction

Arithmetic operators help you do math with numbers in your program. They let you add, subtract, multiply, and divide values easily.

Calculating the total price of items in a shopping cart.
Finding the average score of a student.
Increasing or decreasing a counter in a game.
Splitting a bill evenly among friends.
Converting units, like inches to centimeters.
Syntax
C Sharp (C#)
result = value1 + value2;  // addition
result = value1 - value2;  // subtraction
result = value1 * value2;  // multiplication
result = value1 / value2;  // division
result = value1 % value2;  // remainder (modulus)

Use + to add, - to subtract, * to multiply, / to divide, and % to get the remainder.

Division between integers gives an integer result (no decimals). Use floating-point numbers for decimal results.

Examples
Adds 5 and 3 to get 8.
C Sharp (C#)
int sum = 5 + 3;  // sum is 8
Subtracts 4 from 10 to get 6.
C Sharp (C#)
int difference = 10 - 4;  // difference is 6
Multiplies 7 by 6 to get 42.
C Sharp (C#)
int product = 7 * 6;  // product is 42
Divides 20 by 4 to get 5, and finds remainder of 20 divided by 3 is 2.
C Sharp (C#)
int quotient = 20 / 4;  // quotient is 5
int remainder = 20 % 3;  // remainder is 2
Sample Program

This program shows how to use all the basic arithmetic operators with two numbers, 15 and 4. It prints the results for each operation.

C Sharp (C#)
using System;

class Program
{
    static void Main()
    {
        int a = 15;
        int b = 4;

        int sum = a + b;
        int difference = a - b;
        int product = a * b;
        int quotient = a / b;
        int remainder = a % b;

        Console.WriteLine($"Sum: {sum}");
        Console.WriteLine($"Difference: {difference}");
        Console.WriteLine($"Product: {product}");
        Console.WriteLine($"Quotient: {quotient}");
        Console.WriteLine($"Remainder: {remainder}");
    }
}
OutputSuccess
Important Notes

Remember that dividing two integers will cut off decimals. For example, 15 / 4 is 3, not 3.75.

Use double or float types if you want decimal results.

The modulus operator % gives the remainder after division, useful for checking if a number is even or odd.

Summary

Arithmetic operators let you do basic math in your code.

Use +, -, *, /, and % for addition, subtraction, multiplication, division, and remainder.

Integer division drops decimals; use floating-point types for precise division.