PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Arithmetic Operators in C: +, -, *, /, % (Modulo Rules) (अंकगणितीय ऑपरेटर्स: जोड़, घटाव, गुणा, भाग और मॉड्यूलो (%) नियम) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Arithmetic operators in C (+, -, *, /, %) perform mathematical computations using strict integer truncation, C99 dividend-sign modulo rules, and automatic type promotions. Understanding signed overflow Undefined Behavior and integer-vs-floating division prevents subtle calculation bugs.
अध्याय सार: C में अंकगणितीय ऑपरेटर्स (+, -, *, /, %) पूर्णांक विभाजन ट्रंकेशन, C99 डिविडेंड-साइन मॉड्यूलो नियम और ऑटोमैटिक टाइप प्रमोशन के अनुसार गणना करते हैं। साइन्ड ओवरफ्लो (Undefined Behavior) व इंटीजर बनाम फ्लोटिंग भाग को समझना त्रुटिरहित प्रोग्रामिंग के लिए आवश्यक है।
Arithmetic Operators in C: +, -, *, /, % (Modulo Rules): Detailed Notes & Architecture (अंकगणितीय ऑपरेटर्स: जोड़, घटाव, गुणा, भाग और मॉड्यूलो (%) नियम)
1. Binary & Unary Arithmetic Operators and Integer Division (बाइनरी व यूनरी अंकगणितीय ऑपरेटर्स और पूर्णांक विभाजन)
[EN] C provides five binary arithmetic operators—addition (+), subtraction (-), multiplication (*), division (/), and modulo (%)—alongside two unary arithmetic operators (+ and -). When both operands of / are integers, C performs integer division and discards the fractional part completely. Prior to C99, truncating negative quotients was implementation-defined, whereas C99 and later ISO C standards mandate truncation toward zero (so -7 / 2 always yields -3). Binary multiplicative operators (*, /, %) share higher precedence than additive operators (+, -) and associate strictly from left to right.
[HI] C भाषा में पांच बाइनरी अंकगणितीय ऑपरेटर्स (+, -, *, /, %) तथा दो यूनरी ऑपरेटर्स (+ और -) होते हैं। जब / ऑपरेटर के दोनों ऑपरेंड पूर्णांक (Integer) हों, तो C दशमलव भाग को पूरी तरह हटा देता है। C89 में ऋणात्मक संख्याओं के विभाजन का राउंडिंग नियम कंपाइलर-निर्भर था, परंतु C99 व उसके बाद के मानकों में शून्य की ओर ट्रंकेशन (Truncation toward zero) अनिवार्य है, जिससे -7 / 2 हमेशा -3 देता है। गुणन श्रेणी (*, /, %) की प्राथमिकता जोड़-घटाव (+, -) से अधिक होती है और इनका साहचर्य बाएँ से दाएँ होता है।
2. Modulo Operator (%) Constraints and Sign Rules (मॉड्यूलो (%) ऑपरेटर की शर्तें व चिह्न नियम)
[EN] The modulo operator (%) returns the integer remainder after division and works strictly on integer operands (char, short, int, long, long long); applying % to float or double triggers a compile-time error unless using fmod() from <math.h>. In ISO C99 and newer standards, the quotient and remainder satisfy the algebraic identity (a / b) * b + (a % b) == a whenever a / b is representable. Consequently, the sign of the remainder (a % b) always matches the sign of the dividend (left operand a): -7 % 3 evaluates to -1, while 7 % -3 evaluates to +1. Notice how this differs from Python, where modulo output matches the sign of the divisor.
[HI] मॉड्यूलो ऑपरेटर (%) भाग देने के बाद शेषफल (Remainder) लौटाता है और यह केवल पूर्णांक ऑपरेंड पर ही काम करता है; float या double पर % लगाने से कंपाइल-टाइम एरर आता है (फ्लोट के लिए <math.h> का fmod() उपयोग होता है)। C99 और बाद के मानकों में समीकरण (a / b) * b + (a % b) == a लागू होता है, इसलिए शेषफल का चिह्न हमेशा भाज्य (बाएँ ऑपरेंड a) के चिह्न जैसा होता है—जैसे -7 % 3 का मान -1 और 7 % -3 का मान +1 आता है। यह नियम पायथन से भिन्न है जहाँ शेषफल का चिह्न भाजक (Divisor) पर निर्भर करता है।
3. Integer Promotion and Usual Arithmetic Conversions (इंटीजर प्रमोशन और सामान्य अंकगणितीय रूपांतरण)
[EN] Before executing an arithmetic operation, C applies Integer Promotion to small integer types: char, signed char, unsigned char, and short (along with bit-fields) are automatically promoted to int (or unsigned int if int cannot represent all values). Next, if the two operands still have different types, Usual Arithmetic Conversions promote the narrower operand along the hierarchy int -> unsigned int -> long -> unsigned long -> long long -> float -> double -> long double. Mixing signed and unsigned integers of equal or higher unsigned rank converts the negative signed value modulo 2^N into a huge positive unsigned number, producing unexpected results such as (-5 + 2U) wrapping near UINT_MAX.
[HI] किसी भी अंकगणितीय गणना से पहले C छोटे पूर्णांक प्रकारों (char, short आदि) पर इंटीजर प्रमोशन लागू कर उन्हें int में बदलता है। यदि दोनों ऑपरेंड का डेटा टाइप अभी भी भिन्न हो, तो Usual Arithmetic Conversions छोटे टाइप को बड़े टाइप (int से double/long double तक) में रूपांतरित कर देता है। यदि समान या उच्च रैंक के unsigned टाइप के साथ ऋणात्मक signed मान जोड़ा या भाग किया जाए, तो वह ऋणात्मक मान मॉड्यूलो 2^N के कारण बहुत बड़ी धनात्मक संख्या में बदल जाता है, जो कई प्रोग्रामिंग बग्स का कारण बनता है।
4. Overflow Undefined Behavior, Division by Zero & Edge Cases (ओवरफ्लो अनडिफाइंड बिहेवियर, शून्य से भाग व विशेष स्थितियाँ)
[EN] Dividing or taking modulo by zero (x / 0 or x % 0) causes Undefined Behavior (UB) on integers and typically raises a hardware SIGFPE runtime crash. Signed integer overflow (such as INT_MAX + 1) is also Undefined Behavior in C, allowing optimizing compilers to assume signed overflow never happens, whereas unsigned arithmetic wraps deterministically modulo 2^width. A notorious corner case in two's complement arithmetic is INT_MIN / -1 and INT_MIN % -1: because |INT_MIN| exceeds INT_MAX by 1 (e.g., +2147483648 cannot fit in 32-bit signed int), both expressions invoke Undefined Behavior.
[HI] किसी पूर्णांक को शून्य से भाग देना (x / 0) या x % 0 करना अनडिफाइंड बिहेवियर (UB) है, जिससे प्रोग्राम रनटाइम पर क्रैश (SIGFPE) हो सकता है। साइन्ड इंटीजर ओवरफ्लो (जैसे INT_MAX + 1) भी C में Undefined Behavior है, जबकि अनसाइन्ड ओवरफ्लो हमेशा 2^width के मॉड्यूलो नियम से सुरक्षित रूप से रैप-अराउंड होता है। टूज़ कॉम्प्लिमेंट सिस्टम में INT_MIN / -1 और INT_MIN % -1 भी UB उत्पन्न करते हैं क्योंकि +2147483648 32-बिट साइन्ड int की अधिकतम सीमा (2147483647) से बाहर होता है।
Syntax, Formula & Technical Example (Arithmetic Operators in C: +, -, *, /, % (Modulo Rules))
#include <stdio.h>
int main() {
int a = -7, b = 3;
printf("div=%d mod=%d ", a / b, a % b);
printf("pos_mod=%d ", 7 % -3);
printf("flt=%.1f\n", 7 / 2.0);
return 0;
}
// Output: div=-2 mod=-1 pos_mod=1 flt=3.5
Arithmetic Operators in C: +, -, *, /, % (Modulo Rules) Quick Reference & Comparison Table
| Operator / Rule | Technical Specification | C99+ Exam Point |
|---|---|---|
| Integer Division (/) | Truncates quotient toward zero on integer operands | Result of -7 / 2 is -3 (fractional part dropped) |
| Modulo Remainder (%) | Accepts integers only; sign matches dividend | -7 % 3 yields -1 while 7 % -3 yields +1 |
| Floating Modulo fmod() | Computes remainder for double via <math.h> | Replaces % because % fails on float/double |
| Unary + and - Operators | Promotes char/short to int; unary - negates value | sizeof(+'A') equals sizeof(int) due to promotion |
Arithmetic Operators in C: +, -, *, /, % (Modulo Rules) Top 20 Bilingual MCQs with Answers
Q1. What happens when you compile the expression `5.5 % 2.0` in standard C?
(मानक C में `5.5 % 2.0` एक्सप्रेशन को कंपाइल करने पर क्या होता है?)
(A) Outputs 1 after implicit int cast
(B) Triggers Undefined Behavior at runtime
(C) Outputs 1.5
(D) Compilation error because % accepts only integer operands
Correct Answer: (D)
[EN] The C modulo operator (%) requires both operands to have integer type; floating-point remainders require fmod() from <math.h>.
[HI] C में मॉड्यूलो ऑपरेटर (%) केवल पूर्णांक ऑपरेंड स्वीकार करता है; फ्लोट या डबल मान पर इसका प्रयोग कंपाइलर त्रुटि देता है।
Q2. Under the C99/C11 standard, what is the exact value of `-17 % 5`?
(C99/C11 मानक के अनुसार `-17 % 5` का सटीक मान क्या होगा?)
(A) -2
(B) 2
(C) 3
(D) -3
Correct Answer: (A)
[EN] -17 / 5 truncates toward zero to -3, and (-3)*5 + (-17 % 5) = -17 gives -2; sign matches the dividend (-17).
[HI] -17 / 5 शून्य की ओर ट्रंकेट होकर -3 बनता है और शेषफल का चिह्न भाज्य (-17) के समान होने से -2 प्राप्त होता है।
Q3. What does `17 % -5` evaluate to in ISO C99 and later?
(ISO C99 और उसके बाद के मानकों में `17 % -5` का परिणाम क्या होता है?)
(A) 2
(B) -3
(C) 3
(D) -2
Correct Answer: (A)
[EN] 17 / -5 = -3; (-3)*(-5) = 15, leaving remainder +2 matching the positive dividend 17.
[HI] 17 / -5 = -3 होता है और (-3)*(-5) + 2 = 17 होने से शेषफल +2 मिलता है (भाज्य के चिह्न अनुसार)।
Q4. What is the output of `printf("%.1f", 7 / 2 * 1.0);` in C?
(C में `printf("%.1f", 7 / 2 * 1.0);` का आउटपुट क्या होगा?)
(A) 3
(B) 3.5
(C) 3.0
(D) 4.0
Correct Answer: (C)
[EN] / and * share equal precedence and associate left-to-right: integer 7 / 2 evaluates first to 3, then 3 * 1.0 yields 3.0.
[HI] / और * की प्राथमिकता समान (बाएँ से दाएँ) है, अतः पहले पूर्णांक भाग 7 / 2 = 3 होता है और फिर 3 * 1.0 से 3.0 प्रिंट होता है।
Q5. Which standard library function computes the floating-point remainder in C?
(C में फ्लोटिंग-पॉइंट संख्याओं का शेषफल निकालने के लिए किस लाइब्रेरी फंक्शन का उपयोग किया जाता है?)
(A) remf() in <stdio.h>
(B) mod() in <stdlib.h>
(C) fmod() declared in <math.h>
(D) div() in <math.h>
Correct Answer: (C)
[EN] The <math.h> header provides fmod(double x, double y), fmodf() for float, and fmodl() for long double.
[HI] <math.h> हेडर में स्थित fmod() (तथा fmodf/fmodl) फ्लोटिंग-पॉइंट विभाजन का शेषफल निकालने के लिए प्रयुक्त होता है।
Q6. Given `char c = 'A';` on a system with 4-byte `int`, what does `sizeof(+c)` evaluate to?
(4-बाइट `int` वाले सिस्टम पर `char c = 'A';` के लिए `sizeof(+c)` का मान क्या होगा?)
(A) 1
(B) 4
(C) 2
(D) 8
Correct Answer: (B)
[EN] Applying the unary + operator triggers integer promotion, converting char c into an int before sizeof inspects the expression type.
[HI] यूनरी + ऑपरेटर लगाने पर char पर इंटीजर प्रमोशन लागू होकर उसका टाइप int हो जाता है, अतः sizeof 4 लौटाता है।
Q7. What is the value of `10 + 20 * 3 / 4 - 2` in C?
(C में `10 + 20 * 3 / 4 - 2` व्यंजक का मान क्या होगा?)
(A) 30
(B) 23
(C) 20
(D) 13
Correct Answer: (B)
[EN] Multiplicative operators run left-to-right first: 20 * 3 = 60, 60 / 4 = 15; then 10 + 15 - 2 = 23.
[HI] पहले * और / बाएँ से दाएँ हल होते हैं: 20 * 3 = 60, फिर 60 / 4 = 15; अंत में 10 + 15 - 2 = 23 मिलता है।
Q8. Which algebraic identity holds true in C99 for any integers `a` and `b != 0` when `a / b` does not overflow?
(C99 में जब `a / b` ओवरफ्लो न हो, तो किसी भी पूर्णांक `a` और `b != 0` के लिए कौन सा समीकरण सदैव सत्य है?)
(A) (a / b) * b - (a % b) == a
(B) (a % b) * b + (a / b) == a
(C) a % b >= 0 always
(D) (a / b) * b + (a % b) == a
Correct Answer: (D)
[EN] ISO C guarantees quotient*divisor + remainder equals dividend, forcing a % b to share the sign of a.
[HI] ISO C में भागफल * भाजक + शेषफल = भाज्य हमेशा सत्य रहता है, जिससे a % b का चिह्न a के समान होता है।
Q9. On a standard 32-bit two's complement system, why is `INT_MIN / -1` considered Undefined Behavior in C?
(मानक 32-बिट टूज़ कॉम्प्लिमेंट सिस्टम पर C में `INT_MIN / -1` को अनडिफाइंड बिहेवियर क्यों माना जाता है?)
(A) Because / only works on positive integers
(B) Because C forbids dividing by negative numbers
(C) Because +2147483648 overflows signed 32-bit int maximum (2147483647)
(D) Because INT_MIN is an unsigned constant
Correct Answer: (C)
[EN] In 32-bit two's complement, INT_MIN is -2^31 (-2147483648) while INT_MAX is 2^31 - 1 (2147483647), so negating INT_MIN overflows signed int.
[HI] 32-बिट टूज़ कॉम्प्लिमेंट में INT_MIN = -2147483648 होता है और इसका धनात्मक मान +2147483648, INT_MAX (2147483647) से अधिक होने के कारण ओवरफ्लो कर जाता है।
Q10. What happens when an `unsigned int` overflows after `UINT_MAX + 1U` in C?
(C में `UINT_MAX + 1U` करने पर अनसाइन्ड पूर्णांक ओवरफ्लो होने से क्या परिणाम मिलता है?)
(A) It raises a mandatory compile error
(B) It saturates and stays at UINT_MAX
(C) It wraps cleanly around to 0 using modulo 2^N arithmetic
(D) It invokes Undefined Behavior just like signed overflow
Correct Answer: (C)
[EN] Unlike signed overflow (UB), ISO C defines unsigned integer arithmetic strictly modulo 2^width, wrapping UINT_MAX + 1U to 0.
[HI] साइन्ड ओवरफ्लो के विपरीत, अनसाइन्ड अंकगणित मॉड्यूलो 2^N के अनुसार परिभाषित है, इसलिए UINT_MAX + 1U सुरक्षित रूप से 0 बन जाता है।
Q11. In a circular buffer of size `N = 8`, which expression correctly advances index `i` to the next slot wrapping from 7 back to 0?
(`N = 8` आकार के सर्कुलर बफर में इंडेक्स `i` को 7 के बाद वापस 0 पर लाने के लिए कौन सा सूत्र सही है?)
(A) i % 8 + 1
(B) (i + 1) % 8
(C) (i - 1) % 8
(D) (i + 1) / 8
Correct Answer: (B)
[EN] When i is 7, (7 + 1) % 8 = 8 % 8 = 0, cleanly wrapping around the circular array indices 0..7.
[HI] जब i = 7 होगा, तब (7 + 1) % 8 = 0 होकर इंडेक्स पुनः बफर की शुरुआत 0 पर लौट आएगा।
Q12. If `unsigned int u = 1;` and `int s = -2;`, what is the type and rough behavior of `u + s` in C?
(यदि `unsigned int u = 1;` और `int s = -2;` हो, तो C में `u + s` किस प्रकार व्यवहार करेगा?)
(A) Result is signed int -1
(B) s converts to unsigned int (UINT_MAX - 1), yielding UINT_MAX (all 1-bits)
(C) u converts to signed int, yielding -1
(D) Compile error due to signed/unsigned mismatch
Correct Answer: (B)
[EN] Under Usual Arithmetic Conversions, signed int promotes to unsigned int of equal rank, converting -2 to 2^32 - 2 and summing to UINT_MAX.
[HI] Usual Arithmetic Conversions में int को unsigned int में बदला जाता है, जिससे -2 बहुत बड़े अनसाइन्ड मान में बदलकर UINT_MAX परिणाम देता है।
Q13. What value does `1 / 2 + 1 / 2 + 1.0` evaluate to in C?
(C में `1 / 2 + 1 / 2 + 1.0` व्यंजक का मान क्या होगा?)
(A) 2.0
(B) 0.0
(C) 1.5
(D) 1.0
Correct Answer: (D)
[EN] Both 1 / 2 operations use integer operands and evaluate to 0; finally 0 + 0 + 1.0 yields double 1.0.
[HI] दोनों 1 / 2 पूर्णांक विभाजन होने से 0 बनते हैं, और 0 + 0 + 1.0 जुड़कर 1.0 (double) देते हैं।
Q14. To extract the tens digit of a positive integer `n = 478` (getting `7`), which arithmetic expression is correct?
(धनात्मक पूर्णांक `n = 478` का दहाई अंक (`7`) निकालने के लिए कौन सा अंकगणितीय व्यंजक सही है?)
(A) (n % 10) / 10
(B) n / 100
(C) (n / 10) % 10
(D) n % 100
Correct Answer: (C)
[EN] 478 / 10 truncates to 47, and 47 % 10 extracts the remainder 7.
[HI] 478 / 10 से 47 मिलता है और फिर 47 % 10 से दहाई का अंक 7 अलग हो जाता है।
Q15. Which statement is NOT true about arithmetic operators in C?
(C में अंकगणितीय ऑपरेटर्स के बारे में इनमें से कौन सा कथन असत्य है?)
(A) The % operator returns the floating-point remainder when applied to double values
(B) Binary +, -, *, /, % associate from left to right
(C) Integer division in C99 truncates toward zero
(D) Signed integer overflow causes Undefined Behavior
Correct Answer: (A)
[EN] % cannot be applied to float or double in C (it triggers a compiler error); fmod() must be used instead.
[HI] % ऑपरेटर double मानों पर कार्य नहीं करता (इस पर कंपाइल एरर आता है); फ्लोटिंग शेषफल के लिए fmod() उपयोग होता है।
Q16. What does `printf("%d", 25 % 7 * 2);` print in C?
(C में `printf("%d", 25 % 7 * 2);` क्या प्रिंट करेगा?)
(A) 1
(B) 11
(C) 8
(D) 4
Correct Answer: (C)
[EN] % and * have equal precedence and left-to-right associativity: 25 % 7 = 4, then 4 * 2 = 8.
[HI] % और * समान प्राथमिकता रखते हैं और बाएँ से दाएँ चलते हैं: पहले 25 % 7 = 4, फिर 4 * 2 = 8 प्राप्त होगा।
Q17. Within Arithmetic Operators in C: +, -, *, /, % (Modulo Rules), which operator / rule directly maps to: "Truncates quotient toward zero on integer operands"?
(Arithmetic Operators in C: +, -, *, /, % (Modulo Rules) में कौन सा Operator / Rule सीधे "Truncates quotient toward zero on integer operands" से संबंधित है?)
(A) Floating Modulo fmod()
(B) Integer Division (/)
(C) Unary + and - Operators
(D) Modulo Remainder (%)
Correct Answer: (B)
[EN] In Arithmetic Operators in C: +, -, *, /, % (Modulo Rules), 'Integer Division (/)' handles 'Truncates quotient toward zero on integer operands' (Result of -7 / 2 is -3 (fractional part dropped)).
[HI] Arithmetic Operators in C: +, -, *, /, % (Modulo Rules) में 'Integer Division (/)' का कार्य/विवरण 'Truncates quotient toward zero on integer operands' (Result of -7 / 2 is -3 (fractional part dropped)) है।
Q18. Which option accurately describes 'Modulo Remainder (%)' in Arithmetic Operators in C: +, -, *, /, % (Modulo Rules)?
(Arithmetic Operators in C: +, -, *, /, % (Modulo Rules) के संदर्भ में 'Modulo Remainder (%)' की सही विशेषता कौन सी है?)
(A) Computes remainder for double via <math.h>
(B) Accepts integers only; sign matches dividend
(C) Promotes char/short to int; unary - negates value
(D) Truncates quotient toward zero on integer operands
Correct Answer: (B)
[EN] 'Modulo Remainder (%)' provides 'Accepts integers only; sign matches dividend' — key point: -7 % 3 yields -1 while 7 % -3 yields +1.
[HI] 'Modulo Remainder (%)' का मुख्य विवरण 'Accepts integers only; sign matches dividend' है (-7 % 3 yields -1 while 7 % -3 yields +1)।
Q19. In Arithmetic Operators in C: +, -, *, /, % (Modulo Rules), identify the correct parameter for the specification: "Computes remainder for double via <math.h>".
(Arithmetic Operators in C: +, -, *, /, % (Modulo Rules) में "Computes remainder for double via <math.h>" विवरण किस विकल्प के लिए सही है?)
(A) Integer Division (/)
(B) Modulo Remainder (%)
(C) Floating Modulo fmod()
(D) Unary + and - Operators
Correct Answer: (C)
[EN] 'Floating Modulo fmod()' corresponds to 'Computes remainder for double via <math.h>' (Replaces % because % fails on float/double).
[HI] 'Floating Modulo fmod()' का संबंध 'Computes remainder for double via <math.h>' (Replaces % because % fails on float/double) से है।
Q20. Considering Arithmetic Operators in C: +, -, *, /, % (Modulo Rules), what key behavior defines 'Unary + and - Operators'?
(Arithmetic Operators in C: +, -, *, /, % (Modulo Rules) में 'Unary + and - Operators' की मुख्य भूमिका या मान क्या है?)
(A) Computes remainder for double via <math.h>
(B) Truncates quotient toward zero on integer operands
(C) Accepts integers only; sign matches dividend
(D) Promotes char/short to int; unary - negates value
Correct Answer: (D)
[EN] 'Unary + and - Operators' represents 'Promotes char/short to int; unary - negates value' (sizeof(+'A') equals sizeof(int) due to promotion).
[HI] 'Unary + and - Operators' का अर्थ/मान 'Promotes char/short to int; unary - negates value' है (sizeof(+'A') equals sizeof(int) due to promotion)।
Arithmetic Operators in C: +, -, *, /, % (Modulo Rules) — Exam FAQs
Q1: Why does the modulo (%) operator fail on float or double in C, and what is the alternative?
C में float या double पर % ऑपरेटर क्यों काम नहीं करता और इसका विकल्प क्या है?
[EN] ISO C restricts % strictly to integer types because bit-exact division remainders differ for IEEE 754 floating-point representations. Use fmod(x, y) from <math.h> (and link with -lm on older toolchains) to get floating-point remainders.
[HI] ISO C में % केवल पूर्णांक प्रकारों तक सीमित है क्योंकि फ्लोटिंग-पॉइंट में शेषफल की परिभाषा अलग होती है। दशमलव संख्याओं का शेषफल निकालने के लिए <math.h> के fmod(x, y) फंक्शन का उपयोग किया जाता है।
Q2: Why does -7 % 3 give -1 in C99 while Python gives +2?
C99 में -7 % 3 का मान -1 क्यों आता है जबकि पायथन में +2 आता है?
[EN] C99 defines integer division using truncation toward zero (-7 / 3 = -2) so remainder a % b takes the sign of the dividend (-1). Python uses floor division toward negative infinity (-7 // 3 = -3), making remainder match the divisor (+2).
[HI] C99 में विभाजन शून्य की ओर ट्रंकेट होता है जिससे शेषफल भाज्य के चिह्न (-1) जैसा रहता है, जबकि पायथन फ्लोर डिवीजन का उपयोग करता है जिससे शेषफल भाजक के चिह्न (+2) जैसा आता है।
Q3: How can I safely avoid integer division truncation when computing percentages like marks/total * 100?
प्रतिशत निकालते समय पूर्णांक विभाजन ट्रंकेशन (जैसे 45 / 50 * 100 = 0) से कैसे बचें?
[EN] Cast at least one operand to double before dividing—e.g., (double)marks / total * 100—or multiply by 100.0 first (marks * 100.0 / total) so floating-point division occurs instead of integer division.
[HI] भाग देने से पहले कम से कम एक ऑपरेंड को (double) में टाइप-कास्ट करें जैसे (double)marks / total * 100, या पहले 100.0 से गुणा करें ताकि फ्लोटिंग विभाजन हो सके।
Post a Comment
Post a Comment