โž• C++ Operators
Estimated reading: 2 minutes 41 views

โž— C++ Arithmetic Operators โ€“ Perform Basic Mathematical Operations


๐Ÿงฒ Introduction โ€“ What Are Arithmetic Operators in C++?

Arithmetic operators in C++ are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulus. These operators work with numeric data types and are essential in calculations, loops, algorithms, and data processing.

๐ŸŽฏ In this guide, youโ€™ll learn:

  • List of arithmetic operators
  • Syntax and usage examples
  • Operator precedence rules
  • Common pitfalls and best practices

โž• List of C++ Arithmetic Operators

OperatorSymbolDescriptionExampleResult
Addition+Adds two valuesa + b15
Subtraction-Subtracts second value from firsta - b5
Multiplication*Multiplies two valuesa * b50
Division/Divides numerator by denominatora / b2 (int)
Modulus%Returns remaindera % b0

๐Ÿ“Œ Where int a = 10; int b = 5;


โœ๏ธ Example โ€“ Basic Arithmetic

#include <iostream>
using namespace std;

int main() {
    int a = 15, b = 4;

    cout << "Addition: " << a + b << endl;
    cout << "Subtraction: " << a - b << endl;
    cout << "Multiplication: " << a * b << endl;
    cout << "Division: " << a / b << endl;
    cout << "Modulus: " << a % b << endl;

    return 0;
}

Output:

Addition: 19  
Subtraction: 11  
Multiplication: 60  
Division: 3  
Modulus: 3  

๐Ÿ”บ Operator Precedence (High to Low)

  1. *, /, %
  2. +, -
int result = 10 + 5 * 2;  // 10 + (5 * 2) = 20

โš ๏ธ Common Mistakes

โŒ Mistakeโœ… Fix
Division by zeroEnsure denominator is not zero
Integer division with float resultUse typecasting: (float)a / b
Using % with floatsOnly works with integers in standard C++

๐Ÿ”„ Increment and Decrement (Unary Arithmetic)

These are closely related to arithmetic operators:

int x = 5;
x++;  // Post-increment
++x;  // Pre-increment

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

๐Ÿ” Key Takeaways:

  • Arithmetic operators perform essential mathematical operations
  • % returns remainder and works with integers only
  • Operator precedence impacts the result of expressions
  • Use typecasting for accurate floating-point division

โš™๏ธ Real-World Relevance:
From calculations in financial systems to physics engines in games, arithmetic operators are core tools in C++ programming.


โ“ FAQs โ€“ C++ Arithmetic Operators

โ“ Can I use % with float or double in C++?
โŒ No. Use fmod() from <cmath> instead.

โ“ What happens in integer division?
โœ… Decimal parts are truncated. 5 / 2 gives 2.

โ“ How to ensure precise division?
โœ… Typecast operands:

float result = (float)a / b;

โ“ What is the precedence of * and +?
โœ… * has higher precedence than +, so it’s evaluated first.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

C++ Arithmetic Operators

Or Copy Link

CONTENTS
Scroll to Top