Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to perform all Basic Arithmetic Operations
Basic arithmetic operators in C# allow you to perform mathematical calculations such as addition, subtraction, multiplication, division, and modulus operations. These operators work with numeric data types and form the foundation of mathematical computations in C#.
Arithmetic Operators
| Operator | Description |
|---|---|
| + | Adds two operands |
| - | Subtracts the second operand from the first |
| * | Multiplies both operands |
| / | Divides the numerator by denominator |
| % | Modulus operator returns the remainder after division |
| ++ | Increment operator increases integer value by one |
| -- | Decrement operator decreases integer value by one |
Syntax
Following is the basic syntax for arithmetic operations −
result = operand1 + operand2; // Addition result = operand1 - operand2; // Subtraction result = operand1 * operand2; // Multiplication result = operand1 / operand2; // Division result = operand1 % operand2; // Modulus
Using Basic Arithmetic Operations
Example
using System;
class ArithmeticOperations {
static void Main(string[] args) {
int num1 = 50;
int num2 = 25;
int result;
// Addition
result = num1 + num2;
Console.WriteLine("Addition: {0} + {1} = {2}", num1, num2, result);
// Subtraction
result = num1 - num2;
Console.WriteLine("Subtraction: {0} - {1} = {2}", num1, num2, result);
// Multiplication
result = num1 * num2;
Console.WriteLine("Multiplication: {0} * {1} = {2}", num1, num2, result);
// Division
result = num1 / num2;
Console.WriteLine("Division: {0} / {1} = {2}", num1, num2, result);
// Modulus
result = num1 % num2;
Console.WriteLine("Modulus: {0} % {1} = {2}", num1, num2, result);
}
}
The output of the above code is −
Addition: 50 + 25 = 75 Subtraction: 50 - 25 = 25 Multiplication: 50 * 25 = 1250 Division: 50 / 25 = 2 Modulus: 50 % 25 = 0
Using Increment and Decrement Operators
Example
using System;
class IncrementDecrement {
static void Main(string[] args) {
int number = 10;
Console.WriteLine("Original value: " + number);
// Post-increment
Console.WriteLine("Post-increment: " + number++);
Console.WriteLine("After post-increment: " + number);
// Pre-increment
Console.WriteLine("Pre-increment: " + ++number);
// Post-decrement
Console.WriteLine("Post-decrement: " + number--);
Console.WriteLine("After post-decrement: " + number);
// Pre-decrement
Console.WriteLine("Pre-decrement: " + --number);
}
}
The output of the above code is −
Original value: 10 Post-increment: 10 After post-increment: 11 Pre-increment: 12 Post-decrement: 12 After post-decrement: 11 Pre-decrement: 10
Working with Different Data Types
Example
using System;
class DataTypeArithmetic {
static void Main(string[] args) {
double num1 = 15.5;
double num2 = 4.2;
Console.WriteLine("Double Addition: {0} + {1} = {2}", num1, num2, num1 + num2);
Console.WriteLine("Double Division: {0} / {1} = {2:F2}", num1, num2, num1 / num2);
float f1 = 10.5f;
float f2 = 3.2f;
Console.WriteLine("Float Multiplication: {0} * {1} = {2:F1}", f1, f2, f1 * f2);
// Integer division vs floating-point division
int a = 7, b = 2;
Console.WriteLine("Integer division: {0} / {1} = {2}", a, b, a / b);
Console.WriteLine("Floating division: {0} / {1} = {2:F1}", a, b, (double)a / b);
}
}
The output of the above code is −
Double Addition: 15.5 + 4.2 = 19.7 Double Division: 15.5 / 4.2 = 3.69 Float Multiplication: 10.5 * 3.2 = 33.6 Integer division: 7 / 2 = 3 Floating division: 7 / 2 = 3.5
Conclusion
Arithmetic operators in C# provide the foundation for mathematical calculations in programs. Understanding the difference between integer and floating-point division, as well as pre and post increment/decrement operators, is essential for accurate mathematical computations and program logic.
