#1 India's Top IT Training Institute

×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →

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 मिनट

Input के लिए
scanf()
जोड़ने के लिए
+ operator
Whole numbers के लिए
int (%d)
Decimal numbers के लिए
float (%f)

इस लेख में आप सीखेंगे

Basic Programदो numbers जोड़ने वाला basic C program (input और output के साथ)
Explanationहर line का step-by-step explanation
FunctionFunction वाला cleaner version
AdvancedDecimal numbers, कई numbers और invalid input handle करना

साथ ही: 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

#include <stdio.h> int main() { int num1, num2, sum; printf("Enter first number: "); scanf("%d", &num1); printf("Enter second number: "); scanf("%d", &num2); sum = num1 + num2; printf("Sum of %d and %d is %d\n", num1, num2, sum); return 0; }

Sample Input/Output

Enter first number: 15 Enter second number: 25 Sum of 15 and 25 is 40

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 और पढ़ने में आसान बनता है:

#include <stdio.h> // दो numbers का sum return करने वाला function int add(int a, int b) { return a + b; } int main() { int x, y; printf("Enter two numbers: "); scanf("%d %d", &x, &y); printf("Sum = %d\n", add(x, y)); return 0; }

Sample Input/Output

Enter two numbers: 8 12 Sum = 20

Explanation: add() function दो integers लेता है और उनका sum return करता है। एक ही scanf("%d %d", ...) से दोनों numbers space देकर एक साथ लिए जा सकते हैं।

Decimal (Float) Numbers का Sum

अगर numbers में decimal हो (जैसे 10.5), तो int की जगह float या double use करें:

#include <stdio.h> int main() { float a, b; printf("Enter two decimal numbers: "); scanf("%f %f", &a, &b); printf("Sum = %.2f\n", a + b); return 0; }

Sample Input/Output

Enter two decimal numbers: 10.5 4.25 Sum = 14.75

%f float के लिए format specifier है और %.2f result को 2 decimal places तक दिखाता है।

Loop से N Numbers का Sum

दो से ज्यादा numbers जोड़ने हों, तो loop सबसे अच्छा तरीका है:

#include <stdio.h> int main() { int n, num, sum = 0; printf("How many numbers? "); scanf("%d", &n); for (int i = 1; i <= n; i++) { printf("Enter number %d: ", i); scanf("%d", &num); sum += num; } printf("Total sum = %d\n", sum); return 0; }

Sample Input/Output

How many numbers? 3 Enter number 1: 10 Enter number 2: 20 Enter number 3: 30 Total sum = 60

Invalid Input Handle करना

अगर user number की जगह कोई letter type कर दे, तो scanf() value नहीं पढ़ पाता और variable में garbage value रह सकती है। scanf() का return value check करके इसे handle किया जा सकता है:

#include <stdio.h> int main() { int a, b; printf("Enter two numbers: "); if (scanf("%d %d", &a, &b) != 2) { printf("Invalid input! Please enter numbers only.\n"); return 1; } printf("Sum = %d\n", a + b); return 0; }

Sample Input/Output

Enter two numbers: 5 abc Invalid input! Please enter numbers only.

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 long use करें।

आगे क्या सीखें?

  • दूसरे 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
Website
uncodemy.com