C में दो Numbers का Sum: Program, Input और Output Example (हिंदी में)
संक्षिप्त जवाब: C में दो numbers का sum निकालने के लिए दो int variables
declare करें, scanf("%d", &num) से user से values लें,
sum = num1 + num2; से जोड़ें और printf("%d", sum) से result
दिखाएँ। जैसे input 15 और 25 देने पर output आता है: "Sum of 15 and 25 is 40"।
"दो numbers को जोड़ना" C programming का सबसे पहला practical program है, लेकिन यह छोटा सा program आपको C की पूरी basic structure सिखा देता है: header file, main function, variables, input, operator और output! यही foundation आगे बड़े programs में काम आती है।
लेखक: Rishi Raj · Category: C Programming · पढ़ने का समय: 9 मिनट
इस लेख में आप सीखेंगे
साथ ही: Beginners की common mistakes और FAQs।
C में दो Numbers जोड़ना क्यों सीखें?
यह program छोटा है, लेकिन इससे ये concepts एक साथ सीखे जाते हैं:
- scanf() और printf(): User से input लेना और output दिखाना।
- int data type और %d: Integer values store और print करना।
- + operator: Arithmetic operation।
- Variable declaration: Memory में data रखने की जगह बनाना।
- Program flow: C program ऊपर से नीचे कैसे चलता है।
Basic C Program: दो Numbers का Sum
Sample Input/Output
Program का Line-by-Line Explanation
| Code | मतलब |
|---|---|
#include <stdio.h> |
Standard Input Output header file, जिसमें printf() और scanf() functions होते हैं। |
int main() |
हर C program यहीं से चलना शुरू होता है। |
int num1, num2, sum; |
तीन integer variables declare किए गए। |
printf("Enter first number: "); |
User को message दिखाता है। |
scanf("%d", &num1); |
User को input पढ़कर num1 में store करता है। & variable का
memory address देता है। |
sum = num1 + num2; |
दोनों numbers जोड़कर result sum में रखता है। |
printf("Sum of %d and %d is %d\n", ...) |
तीनों values को format specifiers की जगह print करता है। |
return 0; |
Operating system को बताता है कि program सफलतापूर्वक खत्म हुआ। |
Function के साथ Cleaner Version
बड़े programs में logic को functions में रखना अच्छी practice है। इससे code reusable और पढ़ने में आसान बनता है:
Sample Input/Output
Explanation: add() function दो integers लेता है और उनका sum return
करता
है। एक ही scanf("%d %d", ...) से दोनों numbers space देकर एक साथ लिए जा सकते
हैं।
Decimal (Float) Numbers का Sum
अगर numbers में decimal हो (जैसे 10.5), तो int की जगह float या double use करें:
Sample Input/Output
%f float के लिए format specifier है और
%.2f result को 2 decimal places तक दिखाता है।
Loop से N Numbers का Sum
दो से ज्यादा numbers जोड़ने हों, तो loop सबसे अच्छा तरीका है:
Sample Input/Output
Invalid Input Handle करना
अगर user number की जगह कोई letter type कर दे, तो scanf() value नहीं पढ़ पाता और variable में garbage value रह सकती है। scanf() का return value check करके इसे handle किया जा सकता है:
Sample Input/Output
scanf() सफलतापूर्वक पढ़ी गई values की संख्या return करता है। यहाँ 2 values चाहिए थीं, इसलिए 2 न मिलने पर error message दिखाया गया।
Format Specifiers: कौन सा Data Type, कौन सा Specifier
| Data Type | Format Specifier | Example Value |
|---|---|---|
| int | %d |
25 |
| float | %f |
10.5 |
| double | %lf |
3.14159 |
| char | %c |
'A' |
| long long | %lld |
9876543210 |
Real Life में Addition Logic कहाँ Use होता है?
- Billing systems: Shopping cart का total निकालना।
- Banking: Account balance में deposit जोड़ना।
- Salary calculation: Basic pay, HRA और allowances जोड़ना।
- Inventory: नए stock को मौजूदा stock में जोड़ना।
- Grading systems: Subjects के marks का total।
- Fitness apps: दिन भर के steps या calories जोड़ना।
- Logistics और Games: Distance का हिसाब, game में score बढ़ना।
Beginners की Common Mistakes
- scanf() में & भूल जाना:
scanf("%d", num1)लिखने पर program crash हो सकता है। सही हैscanf("%d", &num1)। - गलत format specifier: float variable के लिए
%dलिखने पर गलत result आता है। - Variable initialize न करना: Loop वाले sum को
sum = 0से शुरू न करने पर garbage value जुड़ जाती है। - Semicolon (;) भूल जाना: C में हर statement के आखिर में semicolon ज़रूरी है।
- Integer overflow: बहुत बड़े numbers के लिए int छोटा पड़ सकता है, तब
long longuse करें।
आगे क्या सीखें?
- दूसरे arithmetic operations: subtraction, multiplication, division और modulus।
- Loops और conditionals (for, while, if-else)।
- Arrays और functions।
- File handling और memory management (pointers)।
C में दो Numbers के Sum से जुड़े FAQs
Q1. C में दो numbers जोड़ने का program beginners के लिए क्यों अच्छा है?
क्योंकि इसमें C program की पूरी basic structure आ जाती है: header file, main function, variables, scanf से input, + operator और printf से output।
Q2. दो numbers जोड़ने के लिए कौन सा data type use करें?
Whole numbers के लिए int और decimal numbers के लिए float या
double, बहुत बड़े integers के लिए long long use करें।
Q3. C में दो से ज्यादा numbers कैसे जोड़ें?
सीधे a + b + c लिख सकते हैं, या ज्यादा numbers के लिए loop चलाकर हर नया
number एक sum variable में जोड़ते जाएँ।
Q4. scanf() में & क्यों लगाते हैं?
& (address-of operator) variable का memory address देता है, ताकि
scanf() user की value सीधे उस variable की memory में लिख सके।
Q5. अगर user number की जगह letter डाल दे तो क्या होगा?
scanf() value नहीं पढ़ पाएगा और variable में garbage value रह सकती है। scanf() का return value check करके invalid input handle करना चाहिए।
Q6. क्या यह addition logic सिर्फ सीखने के लिए है?
नहीं। Billing, banking, salary, inventory, grading और games जैसे लगभग हर real software में यही addition logic use होता है।
Key Takeaways
- Input के लिए scanf(), output के लिए printf() और जोड़ने के लिए + operator use होता है।
- scanf() में variable के आगे & लगाना ज़रूरी है।
- Whole numbers के लिए int (%d) और decimal के लिए float (%f) या double (%lf) use करें।
- Functions से code reusable बनता है और loops से कई numbers जोड़े जा सकते हैं।
- scanf() का return value check करके invalid input handle करें।
निष्कर्ष (Conclusion)
दो numbers जोड़ने का program भले ही छोटा हो, लेकिन यह input handling, data types, operators और output formatting की वह नींव रखता है जिस पर बड़े software projects बनते हैं। इसे अलग-अलग तरीकों से लिखकर देखें: functions के साथ, float के साथ, loop के साथ। हर variation आपकी C की समझ को और मज़बूत करेगा।
C programming को basics से advanced तक सीखना चाहते हैं? Uncodemy का C Programming Course देखें।
Related Hindi Guides
नीचे दिए Hindi pages live होने के बाद इस blog में contextual links जोड़ें — जो page अभी live नहीं है, उसका link न डालें (404 से बचें)।
| Topic | Planned URL |
|---|---|
| Hello World in C | /hi/blog/print-hello-world-in-c-beginner-guide |
| C में Data Types | /hi/blog/what-is-data-type-in-c-types-and-examples |
| C के 32 Keywords | /hi/blog/thirty-two-important-keywords-in-c-language |
Visit Us
- Delhi Centre Address
- 2nd & 3rd Floor, Maa Katyayni Complex, MB-1E, Madhuban Rd, next to DSEU Ambedkar College, Laxmi Nagar, Shakarpur Extension, Shakarpur, Delhi, 110092
- Opening hours
- Monday – Sunday, 06:00 – 22:30
- Phone
- +91 9818366550, +91 8766313646
- info@uncodemy.com
- Website
- uncodemy.com