Arithmetic Operators in C
💻 Arithmetic Operators in C (Easy + Clear Explanation)
In C (developed by Dennis Ritchie), arithmetic operators are used to perform mathematical operations ➕➖✖️➗
🔹 What are Arithmetic Operators? 🤔
These operators are used to:
👉 Add, subtract, multiply, divide numbers
🔹 Types of Arithmetic Operators
| Operator | Name | Example | Meaning |
|---|---|---|---|
+ | Addition | a + b | Add two values |
- | Subtraction | a - b | Subtract |
* | Multiplication | a * b | Multiply |
/ | Division | a / b | Divide |
% | Modulus | a % b | Remainder |
🔹 Examples of Each Operator
🔸 1. Addition (+)
int a = 10, b = 5;
printf("%d", a + b);
👉 Output: 15
🔸 2. Subtraction (-)
printf("%d", a - b);
👉 Output: 5
🔸 3. Multiplication (*)
printf("%d", a * b);
👉 Output: 50
🔸 4. Division (/)
printf("%d", a / b);
👉 Output: 2
👉 ⚠️ Important:
- If both are integers → result is integer
- Decimal part is removed
🔸 5. Modulus (%)
printf("%d", a % b);
👉 Output: 0
👉 Gives remainder
🔹 Complete Program Example 🧠
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("Addition = %d\n", a + b);
printf("Subtraction = %d\n", a - b);
printf("Multiplication = %d\n", a * b);
printf("Division = %d\n", a / b);
printf("Modulus = %d\n", a % b);
return 0;
}
🔹 Output 🎉
Addition = 13
Subtraction = 7
Multiplication = 30
Division = 3
Modulus = 1
🔹 Important Points ⚠️
✔ % works only with integers
✔ Division of integers removes decimal
✔ Use float for decimal division
👉 Example:
float x = 10, y = 3;
printf("%f", x / y); // 3.333333
🔹 Real-Life Example 🤔
👉 + → adding money 💰
👉 - → spending money 💸
👉 * → multiple items 🛒
👉 / → sharing equally 🤝
👉 % → leftover 🍕
🔹 Final Summary 📝
- Arithmetic operators perform math operations
-
+ - * / %are main operators -
%gives remainder - Integer division removes decimals
Comments
Post a Comment