C++ के Arithmetic Operators
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <iostream>
int main() {
std::cout << 7 / 2 << std::endl; // integer division: 3
std::cout << 7.0 / 2 << std::endl; // 3.5
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <iostream>
int main() {
int total = 100;
int price = 20;
total += price; // shorthand for total = total + price
std::cout << total << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
7 / 2जैसे दो ints divide करना और 3.5 की उम्मीद करना, जब result 3 है क्योंकि दोनों operands integers हैं।- doubles पर
%इस्तेमाल करना जैसे5.5 % 2, जो compile नहीं होता;%सिर्फ integers पर काम करता है। - integers के साथ zero से divide करना, जो undefined behavior है और अक्सर crash करता है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: