← Back to C++ Course | Chapter 3: Operators | Lesson 2 of 9

C++ के Arithmetic Operators

Arithmetic operators एक calculator के math buttons हैं: plus, minus, times, divide, और remainder। C++ इन्हें numbers से calculate करने के लिए इस्तेमाल करता है।
Syntax
cpp
result = operand1 + operand2;   // also - * / %

Addition और Subtraction

Addition (+) और subtraction (-) integers और floating-point numbers दोनों पर exactly उम्मीद के मुताबिक काम करते हैं, दो values को combine या offset करते हुए।

ये building blocks हैं जिनसे program में लगभग हर numeric calculation शुरू होता है, चाहे एक cart total करना हो या coordinates में एक difference compute करना।

उदाहरण: Addition and Subtraction

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare sum and set it to 5 + 3
	int sum = 5 + 3;
	// Declare diff and set it to 5 - 3
	int diff = 5 - 3;
	// Print to the console with cout
	std::cout << sum << " " << diff << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Multiplication और Division

Multiplication (*) और division (/) operand types के आधार पर अलग behave करते हैं: दो ints divide करना integer division perform करता है, चुपचाप किसी भी remainder को discard करते हुए, इसलिए 7 / 2 3 evaluate होता है, 3.5 नहीं।

एक fractional result पाने के लिए, कम से कम एक operand को floating-point type होना चाहिए।

उदाहरण: Multiplication and Division

cpp
#include <iostream>

int main() {
	std::cout << 7 / 2 << std::endl;   // integer division: 3
	std::cout << 7.0 / 2 << std::endl; // 3.5
	return 0;
}

Modulo Operator

Modulo operator (%) सिर्फ integer division से बचा हुआ remainder return करता है और दोनों operands को integers होना ज़रूरी बनाता है — उदाहरण के लिए, 10 % 3 1 evaluate होता है।

एक बहुत common practical इस्तेमाल है n % 2 == 0 से even/odd check करना, या array indices में cycle करने जैसी एक fixed range में एक value को wrap करना।

उदाहरण: Modulo Operator

cpp
#include <iostream>

int main() {
	std::cout << 10 % 3 << std::endl; // remainder: 1
	int n = 8;
	std::cout << (n % 2 == 0) << std::endl; // even check
	return 0;
}

Increment और Decrement Operators

++ और -- एक variable को exactly 1 adjust करते हैं, और उनकी position मायने रखती है: prefix (++x) आस-पास के expression में नई value इस्तेमाल होने से पहले variable को increment करता है, जबकि postfix (x++) पहले original value इस्तेमाल करता है और बाद में increment करता है।

यह distinction arr[i++] जैसे बड़े expressions के अंदर सबसे ज़्यादा मायने रखती है, जहाँ increment की timing यह बदल देती है कि कौन सा element access होता है।

उदाहरण: Increment and Decrement Operators

cpp
#include <iostream>

int main() {
	int x = 5;
	std::cout << ++x << std::endl; // prefix: increments first, prints 6
	std::cout << x++ << std::endl; // postfix: prints 6, then becomes 7
	std::cout << x << std::endl;
	return 0;
}

Compound Assignment Operators

+=, -=, *=, और /= जैसे compound assignment operators एक operation और एक assignment को एक step में fold करते हैं, इसलिए total += price; total = total + price; का shorthand है।

Keystrokes बचाने से आगे, यह shorthand 'इस variable को इसकी खुद से जुड़े एक नए expression में reassign करना' की बजाय 'इस variable को update करना' की तरह ज़्यादा clearly पढ़ता है।

उदाहरण: Compound Assignment Operators

cpp
#include <iostream>

int main() {
	int total = 100;
	int price = 20;
	total += price; // shorthand for total = total + price
	std::cout << total << std::endl;
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. 7 / 2 जैसे दो ints divide करना और 3.5 की उम्मीद करना, जब result 3 है क्योंकि दोनों operands integers हैं।
  2. doubles पर % इस्तेमाल करना जैसे 5.5 % 2, जो compile नहीं होता; % सिर्फ integers पर काम करता है।
  3. integers के साथ zero से divide करना, जो undefined behavior है और अक्सर crash करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.