Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🧩 Unit 2 – Problem Solving  ·  Lesson 09

Problem Solving और Algorithms

Problem solving क्या है?

Computer ख़ुद नहीं सोच सकता। उससे कोई problem solve कराने के लिए — bill बनाना, topper ढूँढना, ticket book करना — पहले हमें problem समझकर ठीक-ठीक steps निकालने होते हैं। फिर हम वे steps किसी programming language में लिखते हैं। Problem पहचानने, solution design करने और उसे चलने वाले program में बदलने की पूरी process को problem solving कहते हैं।

🍵 रोज़ का algorithm: चाय बनाना

1. 1 cup पानी उबालें। 2. चायपत्ती और चीनी डालें। 3. दूध डालकर 2 minute उबालें। 4. Cup में छानें। क्रम बदल दें (उबालने से पहले छान लें) तो result ग़लत — algorithm में भी क्रम ज़रूरी है।

Problem solving के steps

1. Problem analyse करेंक्या दिया है (input)? क्या चाहिए (output)?← समझना2. Algorithm बनाएँstep-by-step solution आसान भाषा में← plan3. Codealgorithm को Python में बदलें← बनाना4. Test और debugकई inputs से चलाएँ, errors ठीक करें← जाँचना
Problem solving के चार steps

Example: "5 subjects के marks से student का percentage निकालें।"

  • Input: 5 marks (हर एक 100 में से)।
  • Process: total = marks का जोड़; percentage = total ÷ 500 × 100।
  • Output: total और percentage।

Algorithm क्या है?

Algorithm किसी problem को solve करने के well-defined steps का सीमित क्रम है। यह शब्द Persian गणितज्ञ al-Khwarizmi के नाम से आया है। Algorithm किसी programming language पर निर्भर नहीं — वही algorithm Python, C++ या Java में लिखा जा सकता है।

अच्छे algorithm की विशेषताएँ

📥
Inputशून्य या ज़्यादा साफ़ तय inputs लेता है।
📤
Outputकम से कम एक result देता है।
🎯
Definitenessहर step साफ़ है और उसका एक ही मतलब है।
⏹️
Finitenessसीमित steps के बाद रुक जाता है।
✅
Effectivenessहर step इतना simple कि ठीक-ठीक किया जा सके।
🔁
Correctnessसभी valid inputs पर सही answer।
अच्छे algorithm में ये सब होते हैं

Algorithm examples

Example 1: Rectangle का area

  1. Start
  2. Length L और breadth B input करें
  3. Area ← L × B
  4. Area print करें
  5. Stop

Example 2: Number even है या odd

  1. Start
  2. N input करें
  3. अगर N mod 2 = 0 तो "Even" print करें, नहीं तो "Odd"
  4. Stop

Example 3: तीन numbers में सबसे बड़ा

  1. Start
  2. A, B, C input करें
  3. Largest ← A
  4. अगर B > Largest तो Largest ← B
  5. अगर C > Largest तो Largest ← C
  6. Largest print करें
  7. Stop

Example 4: N का factorial (N! = 1 × 2 × … × N)

  1. Start
  2. N input करें
  3. Fact ← 1, i ← 1
  4. जब तक i ≤ N: Fact ← Fact × i; i ← i + 1
  5. Fact print करें
  6. Stop

Algorithm verify करना (dry run)

Coding से पहले हम algorithm को dry run से verify करते हैं: हम ख़ुद computer बनकर sample input लेते हैं और हर variable की value trace table में लिखते हैं। Example 4 का N = 4 पर dry run:

Stepii ≤ N ?Fact
start1—1
loop 11True1 × 1 = 1
loop 22True1 × 2 = 2
loop 33True2 × 3 = 6
loop 44True6 × 4 = 24
check5False → exit24
Trace table: 4! = 24 ✔

Special cases भी test करें: N = 0 पर 1 आना चाहिए (loop नहीं चलता, Fact 1 ही रहता है ✔), और N = 1 पर 1 ✔।

Algorithms की तुलना

Problem: 1 + 2 + … + N का जोड़। दो सही algorithms:

Algorithm A – loop

  • total ← 0
  • for i = 1 to N: total ← total + i
  • N additions
  • N = 10 लाख → 10 लाख steps

Algorithm B – formula

  • total ← N × (N + 1) ÷ 2
  • सिर्फ़ 3 operations
  • किसी भी N पर same speed
  • साफ़ तौर पर ज़्यादा efficient
दोनों का answer same, पर B बहुत तेज़
Python
def sum_loop(n):          # Algorithm A: add one by one
    total = 0
    for i in range(1, n + 1):
        total += i
    return total

def sum_formula(n):       # Algorithm B: Gauss formula
    return n * (n + 1) // 2

n = 1000000
print(sum_loop(n), sum_formula(n))
print('Loop steps:', n, '| Formula steps: 1')
500000500000 500000500000 Loop steps: 1000000 | Formula steps: 1

एक और example: N prime है या नहीं, यह जाँचने के लिए N − 1 तक नहीं, सिर्फ़ √N तक के divisors test करने होते हैं। N = 10,007 के लिए यह 10,000 की जगह लगभग 100 checks है।

Algorithm से code तक

Example 3 Python में। हर line सीधे एक algorithm step से जुड़ती है।

Python
a = int(input('First number: '))
b = int(input('Second number: '))
c = int(input('Third number: '))
largest = a
if b > largest:
    largest = b
if c > largest:
    largest = c
print('Largest =', largest)
First number: 25 Second number: 71 Third number: 48 Largest = 71

सारांश और practice

  • Problem solving: analyse → algorithm → code → test और debug।
  • Algorithm = input और output वाले सीमित, साफ़, effective steps।
  • Dry run और trace table से verify करें; special cases test करें।
  • Algorithms की तुलना steps (time) और memory से करें।
✅ Practice

Algorithm लिखें: (1) Celsius को Fahrenheit में बदलना (F = C × 9/5 + 32), (2) साल leap year है या नहीं। (2) का 1900, 2000 और 2024 के साथ dry run करें।

Leap year hint: 400 से divisible हो, या 4 से divisible हो पर 100 से नहीं। 1900 → leap नहीं, 2000 → leap, 2024 → leap।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

Algorithm आसान शब्दों में क्या है?
Clear, step-by-step instructions की सीमित list जो कोई input लेकर ज़रूरी output देती है — problem solve करने की recipe जैसी।
Dry run क्या है?
Sample input के साथ algorithm को हाथ से चलाना और हर step के बाद हर variable की value trace table में लिखना, ताकि पता चले output सही है या नहीं।
Algorithms की तुलना क्यों करते हैं?
एक ही problem कई algorithms से solve हो सकती है। हम देखते हैं कि हर एक को कितना time (steps) और memory चाहिए, और सबसे efficient चुनते हैं।
← Class 11 Computer Science
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicFlowchart, Pseudocode और Decomposition आपकी प्रगति: 9 / 29 (31%) · Lesson 10 / 29 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें