⚠ यह कार्रवाई अनुमति नहीं है!
🐍 NIELIT O Level · M3-R5 Python · Hindi Notes

Chapter 2: Algorithms and Flowcharts — समस्या हल करने की कला

किसी भी program के पीछे की सोच — Algorithm (steps) और Flowchart (चित्र)। इस chapter में Sequential, Decision-based व Iterative processing को 10 SVG flowcharts, 20+ solved examples (Factorial, Palindrome, Prime, Fibonacci, Swapping...) के साथ समझेंगे — हर example में Algorithm ➜ Python Code ➜ Output तीनों!

📑 13 Sections 💻 20+ Solved Examples 📊 10 Flowchart Diagrams ❓ 50 MCQs + Output Qs 🗓 Updated 2026
👉 सभी chapters देखने के लिए swipe करें
📑 Table of Contents — किसी भी topic पर सीधे जाएँ
2.0

Algorithm व Flowchart — Programming की नींव

हर program लिखने से पहले 2 काम — Algorithm (steps को शब्दों में लिखना) और Flowchart (उन्हीं steps को चित्र में दिखाना)। फिर code खुद-ब-खुद बन जाता है: Algorithm → Flowchart → Python Code — यही logical development का natural sequence है।

🤔 दोनों की ज़रूरत क्यों?

  • समस्या को systematic तरीके से हल करने के लिए — सीधे code लिखने पर गलतियाँ ज़्यादा होती हैं।
  • Programming logic को पहले से plan व visualize करने के लिए।
  • Errors कम होती हैं, debugging आसान बनती है।
  • Team में logic communicate करना आसान — flowchart सबको समझ आता है।

📖 Quick Recap — Algorithm की 5 Characteristics

Characteristicमतलब
Finitenessसीमित steps में समाप्त हो
Definitenessहर step स्पष्ट व unambiguous
Input0 या अधिक inputs
Outputकम से कम 1 output
Effectivenessहर step simple, manually भी हो सके

🧮 पहला Example — दो Numbers का Sum

Algorithm:
Step 1: Start
Step 2: Read A, B
Step 3: Sum = A + B
Step 4: Display Sum
Step 5: Stop
🐍 Python Equivalent▶ Run
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
sum = a + b
print("Sum =", sum)
OUTPUT (inputs: 25, 17)Sum = 42

⚖️ Algorithm vs Program (नया अंतर!)

AlgorithmProgram
Step-by-step logical planउसी plan का implementation
Language-independentकिसी specific language में (Python)
कोई syntax rules नहींSyntax व structure अनिवार्य
Planning के लिएExecution के लिए
Exam Point: Algorithm = written plan (text); Flowchart = visual plan (diagram); Program = implementation (code)। तीनों का क्रम — पहले algorithm, फिर flowchart, आखिर में code।
2.1

Flowchart Symbols — हर आकृति का मतलब

Flowchart में हर symbol का fixed मतलब होता है — गलत symbol = गलत flowchart! यह table exam में symbol-matching questions का सीधा answer है।

🔣 Complete Symbols Table (7 Symbols)

Symbolनामउपयोग
⬭ OvalTerminal (Start/End)Program की शुरुआत व समाप्ति — इसे Terminal Symbol भी कहते हैं
▭ RectangleProcessCalculation/operation — जैसे A = B + C
▱ ParallelogramInput / OutputData पढ़ना या result दिखाना
◇ DiamondDecisionCondition की जाँच — Yes/No या True/False branching
→ ArrowFlow LineProgram के flow की दिशा
◯ CircleConnectorFlowchart के दो भागों को जोड़ना (बड़े flowcharts में)
🧾 DocumentPrint / ReportOutput को file/document में print करना

✔️ Flowchart बनाने के Best Practices

  • Start व Stop हमेशा Oval में — दोनों अनिवार्य हैं।
  • Flow की दिशा top-to-bottom या left-to-right रखें।
  • हर operation Rectangle में, Input/Output Parallelogram में।
  • Decision (Diamond) से हमेशा 2 रास्ते — Yes व No, दोनों label करें।
  • Arrows एक-दूसरे को cross न करें; flowchart साफ़ व aligned रखें।

🏪 Real-Life Applications

  • ATM transaction — PIN check (decision) → balance check → cash निकालना।
  • Online shopping — cart → payment → order confirmation का flow।
  • Student grading — marks के आधार पर grade (elif ladder)।
  • Bank loan approval — eligibility conditions की जाँच।
Exam Point: Oval = Terminal; Rectangle = Process; Parallelogram = I/O; Diamond = Decision; Circle = Connector — यह matching बार-बार पूछी जाती है। Decision symbol से हमेशा 2 exits निकलती हैं।
2.2

Sequential Processing — सीधी लाइन में चलना

Sequential Processing = statements एक के बाद एक, ऊपर से नीचे क्रम से execute होना — कोई branching (if) नहीं, कोई repetition (loop) नहीं। सबसे simple control structure।

Flowchart 1: Area of Rectangle (Sequential)

Start Input L, B Area = L × B Print Area Stop सीधा रास्ता — न कोई मोड़, न कोई वापसी!

📝 Example 1 — Area of Rectangle

🐍 area_rectangle.py▶ Run
length = float(input("Enter length: "))
breadth = float(input("Enter breadth: "))
area = length * breadth
print("Area of Rectangle =", area)
OUTPUT (inputs: 8, 5)Area of Rectangle = 40.0

📝 Example 2 — Simple Interest

Algorithm: Start → Read P, R, T → SI = (P × R × T) / 100 → Display SI → Stop

Flowchart 2: Simple Interest (Sequential)

Start Input P, R, T SI = (P × R × T) / 100 Print SI Stop
🐍 simple_interest.py▶ Run
P = float(input("Enter Principal: "))
R = float(input("Enter Rate of Interest: "))
T = float(input("Enter Time (in years): "))
SI = (P * R * T) / 100
print("Simple Interest =", SI)
OUTPUT (inputs: 5000, 8, 2)Simple Interest = 800.0

📝 Example 3 — Temperature Conversion (C → F)

🐍 temp_convert.py — Formula: F = (C × 9/5) + 32▶ Run
C = float(input("Enter temperature in Celsius: "))
F = (C * 9/5) + 32
print("Temperature in Fahrenheit =", F)
OUTPUT (input: 37)Temperature in Fahrenheit = 98.6

📝 Example 4 — तीन Numbers का Average

🐍 average.py▶ Run
A = float(input("Enter first number: "))
B = float(input("Enter second number: "))
C = float(input("Enter third number: "))
avg = (A + B + C) / 3
print("Average =", avg)
OUTPUT (inputs: 10, 20, 30)Average = 20.0

📝 Example 5 — Circle का Area व Circumference

🐍 circle.py — दो calculations sequentially▶ Run
R = float(input("Enter radius of circle: "))
area = 3.14 * R * R
circumference = 2 * 3.14 * R
print("Area =", area)
print("Circumference =", circumference)
OUTPUT (input: 7)Area = 153.86
Circumference = 43.96
Exam Point: Sequential = top-to-bottom, no branching, no looping — execution order वही जो लिखा गया। Area, SI, temperature conversion, average — ये सब sequential के classic exam examples हैं।
2.3

Decision Making — Condition के आधार पर रास्ता चुनना

Decision Making (Selection) = condition के आधार पर program का अलग-अलग रास्तों पर जाना। जैसे — marks ≥ 50 तो "Pass", नहीं तो "Fail"। Flowchart में Diamond (◇) symbol, Python में if, if-else, nested if, if-elif-else

Flowchart 3: Even या Odd? (Decision)

Start Input N N % 2 == 0 ?(remainder zero?) Print "Even" Print "Odd" Stop Yes No

✅ 1. Simple if — केवल True पर चलता है

🐍 Syntax + Example▶ Run
# Syntax:
# if condition:
#     statement

marks = int(input("Enter marks: "))
if marks >= 50:
    print("You Passed!")
print("Exam Over.")
OUTPUT (input: 72)You Passed!
Exam Over.
समझें: Condition true (72 ≥ 50) — दोनों lines छपीं। Input 30 होता तो सिर्फ "Exam Over." छपता — if वाली line skip हो जाती, program आगे बढ़ जाता।

✅ 2. if-else — दो रास्ते

🐍 Even या Odd▶ Run
num = int(input("Enter a number: "))
if num % 2 == 0:
    print("Even Number")
else:
    print("Odd Number")
OUTPUT (input: 9)Odd Number

✅ 3. Nested if — if के अंदर if

🐍 Grade Classification — 2 levels की checking▶ Run
marks = int(input("Enter marks: "))
if marks >= 40:
    if marks >= 75:
        print("Distinction")
    else:
        print("Pass")
else:
    print("Fail")
OUTPUT (input: 82)Distinction
समझें: पहले बाहरी if (82 ≥ 40 ✓) अंदर घुसे, फिर भीतरी if (82 ≥ 75 ✓) — "Distinction"। Input 60 पर: बाहरी ✓, भीतरी ✗ → "Pass"। Input 25 पर: बाहरी ही ✗ → "Fail"।

✅ 4. if-elif-else Ladder — कई conditions की सीढ़ी

🐍 Grade System — पहली true condition चलती है▶ Run
marks = int(input("Enter marks: "))
if marks >= 90:
    print("Grade A+")
elif marks >= 75:
    print("Grade A")
elif marks >= 60:
    print("Grade B")
elif marks >= 40:
    print("Grade C")
else:
    print("Fail")
OUTPUT (input: 78)Grade A
ध्यान दें: Ladder में पहली true condition execute होकर बाकी सब skip हो जाती हैं — 78 पर "Grade A" छपा, नीचे वाली conditions checked ही नहीं हुईं (78 ≥ 60 भी true था, पर बारी नहीं आई)।

⚡ Real-Life Example — Electricity Bill

Conditions: 0–100 units → ₹5/unit; 101–200 → ₹7/unit; 200 से ऊपर → ₹10/unit
🐍 electricity_bill.py▶ Run
units = int(input("Enter electricity units used: "))
if units <= 100:
    bill = units * 5
elif units <= 200:
    bill = units * 7
else:
    bill = units * 10
print("Total Bill = Rs.", bill)
OUTPUT (input: 150)Total Bill = Rs. 1050
Exam Point: if = single condition; if-else = 2-way; if-elif-else = multi-way (पहली true चलती है); nested if = multi-level checking। Flowchart में हर decision के लिए अलग Diamond बनता है।
2.4

Looping (Iteration) — बार-बार दोहराना

Looping = किसी block को बार-बार execute करना जब तक condition True रहे। 100 बार "Hello" print करना हो तो 100 lines नहीं — 2 lines का loop! Flowchart में Diamond + पीछे जाता arrow (backward arrow) iteration दिखाता है।

Flowchart 4: Sum of 1 to N (Loop)

Start Input N Sum = 0, i = 1 i ≤ N ? Sum = Sum + i; i = i + 1 Print Sum Stop Yes No ← वापस condition पर!

✅ 1. while Loop — जब तक condition True

🐍 1 से 5 तक print▶ Run
# Syntax:
# while condition:
#     statements

i = 1
while i <= 5:
    print(i)
    i += 1
print("Loop Finished")
OUTPUT1
2
3
4
5
Loop Finished

✅ 2. for Loop — जब repetitions पता हों

🐍 range() के साथ▶ Run
# Syntax:
# for variable in sequence:
#     statements

for i in range(1, 6):
    print(i, end=" ")
print("\nLoop Complete")
OUTPUT1 2 3 4 5
Loop Complete
समझें: range(1, 6) = 1, 2, 3, 4, 5 (आखिरी number शामिल नहीं)। for में initialization, condition, increment — तीनों range() खुद संभाल लेता है; while में तीनों manually लिखने पड़ते हैं।

✅ 3. Sum of First N Natural Numbers

🐍 sum_n.py — ऊपर वाले flowchart का code▶ Run
n = int(input("Enter N: "))
sum = 0
i = 1
while i <= n:
    sum += i
    i += 1
print("Sum =", sum)
OUTPUT (input: 10)Sum = 55

✅ 4. Nested Loop — Loop के अंदर Loop

🐍 Pattern — हर outer के लिए पूरा inner चलता है▶ Run
for i in range(1, 4):         # Outer loop
    for j in range(1, 4):     # Inner loop
        print(i, j)
OUTPUT1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
समझें: Outer i=1 पर inner j पूरा (1,2,3) चला — 3 lines; फिर i=2 पर फिर पूरा; फिर i=3। Total = 3 × 3 = 9 lines। Nested loops pattern printing व matrices में उपयोग होते हैं।

⚖️ while vs for — कब कौन-सा?

Featurewhile Loopfor Loop
Use CaseRepetitions की संख्या पता न होसंख्या पता हो
Conditionहर बार manually checkrange() खुद संभालता है
InitializationLoop से पहले manuallyrange() के अंदर
Exampleसही password आने तक पूछना1 से 10 तक print, list पर चलना
⚠ Infinite Loop: अगर while में counter बढ़ाना (i += 1) भूल गए तो condition कभी false नहीं होगी — loop कभी नहीं रुकेगा! Loop हमेशा controlled होना चाहिए।
Exam Point: while = condition-based (unknown repetitions); for = sequence/range-based (known)। range(1, 6) में 6 शामिल नहीं। Nested loop की total iterations = outer × inner।
2.5

Loop Control Statements — break, continue, pass

कभी loop को बीच में रोकना हो, कभी एक iteration छोड़ना हो — इसके लिए Python देता है 3 statements: break (पूरा loop खत्म), continue (यह iteration skip), pass (कुछ नहीं — placeholder)।

🛑 break — Loop को तुरंत समाप्त

🐍 i == 6 पर loop टूट गया▶ Run
for i in range(1, 10):
    if i == 6:
        break
    print(i, end=" ")
OUTPUT1 2 3 4 5

⏭️ continue — यह iteration छोड़ो, आगे बढ़ो

🐍 i == 3 skip हुआ, loop चलता रहा▶ Run
for i in range(1, 6):
    if i == 3:
        continue
    print(i, end=" ")
OUTPUT1 2 4 5

⬜ pass — कुछ नहीं करता (Placeholder)

🐍 खाली block में error से बचने के लिए▶ Run
for i in range(1, 4):
    if i == 2:
        pass        # baad me code likhenge
    print(i, end=" ")
OUTPUT1 2 3
← pass ने कुछ नहीं बदला — सब print हुए।
Exam Point: break = loop से बाहर; continue = अगले iteration पर; pass = empty statement (कुछ नहीं)। break vs continue का output-difference exam का favourite question है!
2.6

Common Solved Examples — Exam के 10 पक्के Programs

हर example में Algorithm ➜ Python Code ➜ Output — बिल्कुल वैसे जैसे exam की answer-sheet में लिखना है। ये 10 programs O Level में बार-बार आते हैं!

Example 1 — दो Numbers को Swap करना (3rd Variable से)

Algorithm: Start → Input A, B → Temp = A → A = B → B = Temp → Print A, B → Stop

Flowchart 5: Swap Two Numbers (temp variable)

Start Input A, B temp = A A = B B = temp Print A, B Stop temp एक कटोरी है — पहले A उसमें, फिर अदला-बदली!
🐍 swap.py▶ Run
A = int(input("Enter A: "))
B = int(input("Enter B: "))
temp = A
A = B
B = temp
print("After swap: A =", A, ", B =", B)

# Python ka shortcut (bina temp ke):
# A, B = B, A
OUTPUT (inputs: 5, 9)After swap: A = 9 , B = 5

Example 2 — Maximum of Two Numbers

🐍 max_two.py▶ Run
A = int(input("Enter first number: "))
B = int(input("Enter second number: "))
if A > B:
    print("A is greater")
else:
    print("B is greater")
OUTPUT (inputs: 34, 78)B is greater

Example 3 — Factorial of a Number

Algorithm: Start → Input N → Fact = 1, i = 1 → जब तक i ≤ N: Fact = Fact × i, i = i + 1 → Print Fact → Stop

Flowchart 6: Factorial (Loop with backward arrow)

Start Input N Fact = 1, i = 1 i ≤ N ? Fact = Fact × i ; i = i + 1 Print Fact Stop Yes No
🐍 factorial.py▶ Run
N = int(input("Enter a number: "))
Fact = 1
i = 1
while i <= N:
    Fact = Fact * i
    i += 1
print("Factorial =", Fact)
OUTPUT (input: 6)Factorial = 720
Dry Run (N=4): i=1: Fact=1 → i=2: Fact=2 → i=3: Fact=6 → i=4: Fact=24 → i=5 पर condition false → Print 24। यही dry-run exam में trace करके दिखाना होता है।

Example 4 — Reverse a Number

Flowchart 7: Reverse a Number (Loop)

Start Input N Rev = 0 N > 0 ? R = N % 10 ; Rev = Rev × 10 + RN = N // 10 Print Rev Stop Yes No
🐍 reverse.py — % से digit निकालो, // से हटाओ▶ Run
N = int(input("Enter a number: "))
Rev = 0
while N > 0:
    R = N % 10          # aakhri digit
    Rev = Rev * 10 + R
    N = N // 10         # aakhri digit hatao
print("Reversed Number =", Rev)
OUTPUT (input: 6789)Reversed Number = 9876

Example 5 — Palindrome Number Check

Flowchart 8: Palindrome Check (Loop + Decision)

Start Input N Copy = N, Rev = 0 N > 0 ? Rev = Rev × 10 + N % 10N = N // 10 Copy == Rev ?(original = reversed?) Print "Palindrome" Print "Not Palindrome" Stop Yes No Yes No
🐍 palindrome.py — reverse करके original से मिलाओ▶ Run
N = int(input("Enter a number: "))
copy = N
rev = 0
while N > 0:
    r = N % 10
    rev = rev * 10 + r
    N = N // 10
if copy == rev:
    print("Palindrome Number")
else:
    print("Not Palindrome")
OUTPUT (input: 1331)Palindrome Number

Example 6 — Prime Number Check

Flowchart 9: Prime Number Check

Start Input N i = 2 i ≤ N − 1 ? Print "Prime" N % i == 0 ? Print "Not Prime" i = i + 1 Stop No Yes Yes No
🐍 prime.py▶ Run
N = int(input("Enter number: "))
is_prime = True
if N < 2:
    is_prime = False
for i in range(2, N):
    if N % i == 0:
        is_prime = False
        break
if is_prime:
    print(N, "is Prime")
else:
    print(N, "is Not Prime")
OUTPUT (input: 29)29 is Prime

Example 7 — Fibonacci Series

🐍 fibonacci.py — हर term = पिछले दो का sum▶ Run
n = int(input("Kitne terms? "))
a, b = 0, 1
for i in range(n):
    print(a, end=" ")
    a, b = b, a + b
OUTPUT (input: 10)0 1 1 2 3 5 8 13 21 34

Example 8 — Student Grade Evaluation

🐍 grade.py — elif ladder का real use▶ Run
marks = int(input("Enter marks: "))
if marks >= 90:
    grade = "A+"
elif marks >= 75:
    grade = "A"
elif marks >= 60:
    grade = "B"
elif marks >= 40:
    grade = "C"
else:
    grade = "Fail"
print("Grade:", grade)
OUTPUT (input: 88)Grade: A

Example 9 — Smallest of Three Numbers

Flowchart 10: Smallest of Three Numbers (Nested Decision)

Start Input A, B, C A < B ANDA < C ? Print A B < C ? Print B Print C Stop Yes No Yes No
🐍 smallest.py — and operator के साथ▶ Run
A = int(input("Enter first: "))
B = int(input("Enter second: "))
C = int(input("Enter third: "))
if A < B and A < C:
    smallest = A
elif B < C:
    smallest = B
else:
    smallest = C
print("Smallest number =", smallest)
OUTPUT (inputs: 45, 12, 89)Smallest number = 12

Example 10 — Decimal to Binary Conversion

Algorithm: Start → Input N → जब तक N > 0: remainder (N % 2) निकालो, आगे जोड़ो, N = N // 2 → Print binary → Stop
🐍 dec_to_binary.py▶ Run
N = int(input("Enter decimal number: "))
binary = ""
if N == 0:
    binary = "0"
while N > 0:
    binary = str(N % 2) + binary
    N = N // 2
print("Binary =", binary)
OUTPUT (input: 13)Binary = 1101
← Check: 8+4+0+1 = 13 ✓
Exam Point: Swapping (temp variable), Factorial, Reverse, Palindrome, Prime, Fibonacci — इनके algorithm व flowchart बनाने का question लगभग हर exam में। Reverse की trick: % 10 से digit निकालो, // 10 से हटाओ।
2.7

🎯 Output-Based Questions — पहले सोचो, फिर देखो!

Loop tracing व condition वाले output questions exam में पक्के आते हैं। हर question में पहले खुद output निकालिए (dry run करके), फिर answer मिलाइए।

Q1. Range का खेल

🐍 Output बताइए▶ Run
for i in range(2, 12, 3):
    print(i, end=" ")
ANSWER2 5 8 11
← range(start, stop, step): 2 से शुरू, 3-3 बढ़ते हुए, 12 से पहले तक।

Q2. while का Trace

🐍 Output बताइए▶ Run
x = 20
while x > 5:
    print(x, end=" ")
    x = x // 2
ANSWER20 10
← x: 20 (print) → 10 (print) → 5 पर condition false (5 > 5 नहीं), रुक गया।

Q3. break की position

🐍 Output बताइए▶ Run
for i in range(1, 8):
    print(i, end=" ")
    if i == 4:
        break
ANSWER1 2 3 4
← print पहले है, break बाद में — इसलिए 4 print होकर loop टूटा। (अगर break पहले होता तो 1 2 3 आता!)

Q4. continue vs break

🐍 Output बताइए▶ Run
for i in range(1, 6):
    if i % 2 == 0:
        continue
    print(i, end=" ")
ANSWER1 3 5
← Even numbers (2, 4) पर continue चला — print skip; सिर्फ odd print हुए।

Q5. elif Ladder Trace

🐍 Output बताइए▶ Run
x = 85
if x >= 90:
    print("A+")
elif x >= 80:
    print("A")
elif x >= 70:
    print("B")
else:
    print("C")
ANSWERA
← 85 ≥ 90 false; 85 ≥ 80 true → "A" print, बाकी ladder skip (85 ≥ 70 भी true था, पर बारी नहीं आई)।

Q6. Nested Loop की गिनती

🐍 "*" कितनी बार print होगा?▶ Run
for i in range(3):
    for j in range(4):
        print("*", end="")
    print()
ANSWER****
****
****
← 3 × 4 = 12 stars, हर row में 4 (inner), 3 rows (outer)।

Q7. Loop के बाद Variable की Value

🐍 Output बताइए▶ Run
s = 0
for i in range(1, 5):
    s = s + i
print(s)
ANSWER10
← s = 1+2+3+4 = 10 (range में 5 शामिल नहीं)। print loop के बाहर है — सिर्फ final value छपी।

Q8. Reverse Logic Trace

🐍 Output बताइए▶ Run
n = 123
rev = 0
while n > 0:
    rev = rev * 10 + n % 10
    n = n // 10
print(rev)
ANSWER321
← Trace: rev=3, n=12 → rev=32, n=1 → rev=321, n=0 → रुका।

Q9. Infinite Loop पहचानिए

🐍 इस code में क्या गड़बड़ है?▶ Run
i = 1
while i <= 5:
    print(i)
ANSWER1 1 1 1 1 ... (कभी नहीं रुकेगा!)
← i += 1 नहीं लिखा — condition हमेशा true, यह Infinite Loop है।

Q10. Nested if का रास्ता

🐍 Output बताइए▶ Run
a = 10
b = 20
if a > 5:
    if b > 25:
        print("Both big")
    else:
        print("Only a big")
else:
    print("a small")
ANSWEROnly a big
← बाहरी if true (10 > 5) → भीतरी if false (20 > 25 नहीं) → भीतरी else चला।

Q11. Step वाली range उल्टी

🐍 Output बताइए▶ Run
for i in range(10, 0, -2):
    print(i, end=" ")
ANSWER10 8 6 4 2
← Negative step = उल्टी गिनती; 0 शामिल नहीं।

Q12. Swap without Temp

🐍 Output बताइए▶ Run
a, b = 7, 3
a, b = b, a + b
print(a, b)
ANSWER3 10
← दाईं ओर पहले evaluate होता है (b=3, a+b=10), फिर एक साथ assign — a=3, b=10। यह simple swap नहीं है, ध्यान से!
2.8

💻 Practice Programs — खुद Try कीजिए

ये programs solved examples से एक कदम आगे हैं — copy करके run कीजिए, फिर बिना देखे खुद लिखने की कोशिश कीजिए। हर program में loop + condition दोनों का combination है।

Program 1 — Sum of Digits

🐍 sum_digits.py▶ Run
n = int(input("Enter number: "))
total = 0
while n > 0:
    total += n % 10
    n = n // 10
print("Sum of digits =", total)
OUTPUT (input: 456)Sum of digits = 15

Program 2 — Armstrong Number Check (153 = 1³+5³+3³)

🐍 armstrong.py▶ Run
n = int(input("Enter 3-digit number: "))
temp = n
total = 0
while temp > 0:
    d = temp % 10
    total += d ** 3
    temp = temp // 10
if total == n:
    print(n, "is an Armstrong number")
else:
    print(n, "is NOT an Armstrong number")
OUTPUT (input: 153)153 is an Armstrong number

Program 3 — Star Pattern (Right Triangle)

🐍 pattern.py▶ Run
n = int(input("Enter rows: "))
for i in range(1, n + 1):
    print("*" * i)
OUTPUT (input: 4)*
**
***
****

Program 4 — HCF (GCD) of Two Numbers

🐍 hcf.py▶ Run
a = int(input("Enter first: "))
b = int(input("Enter second: "))
hcf = 1
for i in range(1, min(a, b) + 1):
    if a % i == 0 and b % i == 0:
        hcf = i
print("HCF =", hcf)
OUTPUT (inputs: 12, 18)HCF = 6

Program 5 — Count Even/Odd in N Numbers

🐍 count_even_odd.py▶ Run
n = int(input("Kitne numbers? "))
even = odd = 0
for i in range(n):
    num = int(input("Number: "))
    if num % 2 == 0:
        even += 1
    else:
        odd += 1
print("Even count =", even)
print("Odd count =", odd)
OUTPUT (inputs: 5 → 12, 7, 8, 3, 6)Even count = 3
Odd count = 2

Program 6 — Multiplication Table (Loop से)

🐍 table.py▶ Run
num = int(input("Enter a number: "))
for i in range(1, 11):
    print(num, "x", i, "=", num * i)
OUTPUT (input: 9)9 x 1 = 9
9 x 2 = 18
...
9 x 10 = 90

Program 7 — Leap Year Check

🐍 leap_year.py — condition का combination▶ Run
year = int(input("Enter year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
    print(year, "is a Leap Year")
else:
    print(year, "is NOT a Leap Year")
OUTPUT (input: 2024)2024 is a Leap Year

Program 8 — ATM PIN Retry (3 Attempts)

🐍 atm_pin.py — real-life loop + break▶ Run
correct_pin = 1234
attempts = 0
while attempts < 3:
    pin = int(input("Enter PIN: "))
    if pin == correct_pin:
        print("Access Granted!")
        break
    attempts += 1
    print("Wrong PIN! Attempts left:", 3 - attempts)
else:
    print("Card Blocked!")
OUTPUT (inputs: 1111, 2222, 1234)Wrong PIN! Attempts left: 2
Wrong PIN! Attempts left: 1
Access Granted!
Exam Point: Sum of digits, Armstrong, HCF, Leap year — ये M3-R5 के theory व practical दोनों में repeat होने वाले programs हैं। Leap year की condition ((div by 4 AND not by 100) OR div by 400) रटने लायक है!
2.9

Summary — Quick Revision (Exam से पहले पढ़ें)

  • Algorithm = written plan (text steps); Flowchart = visual plan (symbols); Program = implementation।
  • Symbols: Oval = Start/Stop; Rectangle = Process; Parallelogram = I/O; Diamond = Decision; Circle = Connector।
  • Sequential = ऊपर से नीचे सीधा flow (Area, SI, Average); Decision = condition से रास्ता (if-else); Iterative = repetition (loops)।
  • if = 1 condition; if-else = 2-way; if-elif-else = multi-way (पहली true चलती है); nested if = level-wise checking।
  • while = unknown repetitions (condition-based); for = known repetitions (range-based); range(1, 6) में 6 शामिल नहीं।
  • break = loop समाप्त; continue = iteration skip; pass = कुछ नहीं (placeholder)।
  • Nested loop की total iterations = outer × inner; counter न बढ़ाने पर infinite loop
  • याद रखने वाले programs: Swap (temp से), Factorial, Reverse (%10 व //10), Palindrome, Prime, Fibonacci, Armstrong, HCF, Leap Year।
2.10

Model Questions — 50 MCQs + 15 Theory Questions

❓ A. Multiple Choice Questions (50)

#प्रश्न व उत्तर
1

Algorithm है —

(a) Diagram(b) Step-by-step written procedure(c) Language(d) Software
✔ सही उत्तर: (b) Step-by-step written procedure
2

Flowchart है —

(a) Algorithm का graphical रूप(b) Program(c) Compiler(d) Data
✔ सही उत्तर: (a) Algorithm का graphical रूप
3

Algorithm व Flowchart में पहले बनता है —

(a) Flowchart(b) Algorithm(c) Code(d) कोई भी
✔ सही उत्तर: (b) Algorithmपहले plan, फिर diagram।
4

Algorithm होता है —

(a) Language-dependent(b) Language-independent(c) Python-only(d) Binary
✔ सही उत्तर: (b) Language-independent
5

Start/Stop के लिए flowchart symbol —

(a) Rectangle(b) Diamond(c) Oval(d) Circle
✔ सही उत्तर: (c) OvalTerminal symbol।
6

Oval symbol का दूसरा नाम है —

(a) Process(b) Terminal(c) Connector(d) Decision
✔ सही उत्तर: (b) Terminal
7

Process (calculation) के लिए symbol —

(a) Rectangle(b) Oval(c) Diamond(d) Parallelogram
✔ सही उत्तर: (a) Rectangle
8

Input/Output के लिए symbol —

(a) Diamond(b) Circle(c) Parallelogram(d) Oval
✔ सही उत्तर: (c) Parallelogram
9

Decision के लिए symbol —

(a) Diamond(b) Rectangle(c) Arrow(d) Oval
✔ सही उत्तर: (a) Diamond
10

Flowchart के दो भागों को जोड़ता है —

(a) Arrow(b) Connector (Circle)(c) Diamond(d) Document
✔ सही उत्तर: (b) Connector (Circle)
11

Flow की दिशा दिखाता है —

(a) Oval(b) Rectangle(c) Arrow(d) Circle
✔ सही उत्तर: (c) Arrow (Flow Line)
12

Decision symbol से कितने रास्ते निकलते हैं?

(a) 1(b) 2(c) 3(d) 0
✔ सही उत्तर: (b) 2Yes व No।
13

हर flowchart में अनिवार्य हैं —

(a) Loop(b) Start व Stop(c) Decision(d) Connector
✔ सही उत्तर: (b) Start व Stop
14

Flowchart की दिशा होनी चाहिए —

(a) Bottom-to-top(b) Top-to-bottom / Left-to-right(c) Random(d) Circular
✔ सही उत्तर: (b) Top-to-bottom / Left-to-right
15

A = B + C flowchart में लिखा जाएगा —

(a) Oval में(b) Diamond में(c) Rectangle में(d) Circle में
✔ सही उत्तर: (c) Rectangle मेंयह process है।
16

Statements का एक के बाद एक क्रम से चलना —

(a) Decision(b) Sequential Processing(c) Iteration(d) Branching
✔ सही उत्तर: (b) Sequential Processing
17

Sequential structure में नहीं होता —

(a) Input(b) Output(c) Branching/Looping(d) Process
✔ सही उत्तर: (c) Branching/Looping
18

Simple Interest formula है —

(a) P×R×T(b) (P×R×T)/100(c) P+R+T(d) (P+R)/T
✔ सही उत्तर: (b) (P×R×T)/100
19

Celsius से Fahrenheit का formula —

(a) C×5/9+32(b) (C×9/5)+32(c) C+32(d) C×9/5−32
✔ सही उत्तर: (b) (C×9/5)+32
20

Condition के आधार पर रास्ता चुनना कहलाता है —

(a) Sequence(b) Decision Making / Selection(c) Iteration(d) Recursion
✔ सही उत्तर: (b) Decision Making / Selection
21

Python में decision के लिए उपयोग होता है —

(a) loop(b) if, elif, else(c) def(d) import
✔ सही उत्तर: (b) if, elif, else
22

if-elif-else ladder में execute होती है —

(a) सभी true conditions(b) केवल पहली true condition(c) आखिरी condition(d) else हमेशा
✔ सही उत्तर: (b) केवल पहली true condition
23

एक if के अंदर दूसरा if —

(a) Ladder(b) Nested if(c) Loop(d) Chain
✔ सही उत्तर: (b) Nested if
24

num % 2 == 0 check करता है —

(a) Prime(b) Even(c) Positive(d) Palindrome
✔ सही उत्तर: (b) Even
25

if marks >= 50: print("Pass") — marks = 40 पर output —

(a) Pass(b) Fail(c) कुछ नहीं(d) Error
✔ सही उत्तर: (c) कुछ नहींcondition false, else भी नहीं है।
26

बार-बार दोहराई जाने वाली प्रक्रिया —

(a) Sequence(b) Selection(c) Iteration/Looping(d) Declaration
✔ सही उत्तर: (c) Iteration/Looping
27

Python में कितने मुख्य loops हैं?

(a) 1(b) 2 (while, for)(c) 4(d) 5
✔ सही उत्तर: (b) 2 (while, for)
28

Repetitions की संख्या पता न हो तो उपयोग करें —

(a) for(b) while(c) if(d) pass
✔ सही उत्तर: (b) while
29

range(1, 6) देगा —

(a) 1 से 6(b) 1 से 5(c) 0 से 6(d) 2 से 6
✔ सही उत्तर: (b) 1 से 5stop शामिल नहीं।
30

range(10, 0, -2) देगा —

(a) 10 8 6 4 2(b) 10 से 0(c) Error(d) 2 4 6 8 10
✔ सही उत्तर: (a) 10 8 6 4 2
31

Loop को बीच में समाप्त करता है —

(a) continue(b) pass(c) break(d) stop
✔ सही उत्तर: (c) break
32

Current iteration को skip करता है —

(a) break(b) continue(c) pass(d) exit
✔ सही उत्तर: (b) continue
33

कुछ न करने वाला placeholder statement —

(a) break(b) continue(c) pass(d) null
✔ सही उत्तर: (c) pass
34

Counter न बढ़ाने पर while loop बन जाता है —

(a) for loop(b) Infinite loop(c) Nested loop(d) Dead loop
✔ सही उत्तर: (b) Infinite loop
35

Nested loop (outer 3, inner 4) में inner body कुल कितनी बार चलेगी?

(a) 7(b) 12(c) 3(d) 4
✔ सही उत्तर: (b) 123 × 4।
36

Flowchart में loop दिखाने के लिए चाहिए —

(a) दो Ovals(b) Decision + backward arrow(c) केवल Rectangle(d) Connector
✔ सही उत्तर: (b) Decision + backward arrow
37

for i in range(1,10): if i==6: break — print(i) पहले हो तो output —

(a) 1-5(b) 1-6(c) 1-9(d) 6
✔ सही उत्तर: (b) 1 2 3 4 5 6print पहले, break बाद में।
38

Swapping में temp variable का काम —

(a) जोड़ना(b) एक value सुरक्षित रखना(c) घटाना(d) print करना
✔ सही उत्तर: (b) एक value सुरक्षित रखना
39

Python में बिना temp के swap —

(a) a,b = b,a(b) a=b(c) swap(a,b)(d) असंभव
✔ सही उत्तर: (a) a, b = b, a
40

5 का factorial है —

(a) 25(b) 120(c) 60(d) 100
✔ सही उत्तर: (b) 1201×2×3×4×5।
41

Number का आखिरी digit निकालने के लिए —

(a) N // 10(b) N % 10(c) N * 10(d) N + 10
✔ सही उत्तर: (b) N % 10
42

Number से आखिरी digit हटाने के लिए —

(a) N % 10(b) N // 10(c) N - 10(d) N / 2
✔ सही उत्तर: (b) N // 10
43

जो number उल्टा करने पर same रहे —

(a) Prime(b) Armstrong(c) Palindrome(d) Perfect
✔ सही उत्तर: (c) Palindromeजैसे 121, 1331।
44

153 है —

(a) Prime(b) Armstrong number(c) Palindrome(d) Even
✔ सही उत्तर: (b) Armstrong1³+5³+3³ = 153।
45

Fibonacci series का अगला term: 0 1 1 2 3 5 8 ?

(a) 11(b) 13(c) 10(d) 16
✔ सही उत्तर: (b) 135 + 8।
46

Prime check में loop चलता है —

(a) 2 से N-1(b) 0 से N(c) 1 से N(d) N से 1
✔ सही उत्तर: (a) 2 से N-1कहीं divide हुआ तो prime नहीं।
47

Leap year condition है —

(a) year % 4 == 0 only(b) (div by 4 AND not by 100) OR div by 400(c) year % 100 == 0(d) year % 2 == 0
✔ सही उत्तर: (b)इसीलिए 1900 leap नहीं, 2000 है।
48

x=20; while x>5: print(x); x = x//2 — output —

(a) 20 10(b) 20 10 5(c) 20(d) Infinite
✔ सही उत्तर: (a) 20 105 पर condition false।
49

ATM PIN 3-attempts logic में उपयोग होता है —

(a) केवल if(b) loop + break(c) केवल print(d) pass
✔ सही उत्तर: (b) loop + break
50

Algorithm और Program में अंतर —

(a) दोनों same(b) Algorithm plan है, Program implementation(c) Program पहले बनता है(d) Algorithm में syntax होता है
✔ सही उत्तर: (b) Algorithm plan, Program implementation

📝 B. 15 Theory Questions (Short Answers)

  1. Algorithm और Flowchart में अंतर बताइए।Algorithm text/steps में लिखा plan है (language-independent, बनाना आसान); Flowchart उन्हीं steps का symbols वाला diagram है (देखते ही समझ आता है, पर redraw कठिन)।
  2. Flowchart के 7 symbols व उनके उपयोग लिखिए।Oval = Start/Stop (Terminal); Rectangle = Process; Parallelogram = Input/Output; Diamond = Decision; Arrow = Flow line; Circle = Connector; Document = Print/Report।
  3. Sequential Processing क्या है? उदाहरण दीजिए।Statements का एक के बाद एक, ऊपर से नीचे क्रम से चलना — बिना branching/looping के। जैसे Area of Rectangle, Simple Interest, Average निकालना।
  4. Decision Making क्या है? Python में इसके types बताइए।Condition के आधार पर अलग-अलग रास्तों पर जाना। Types — simple if, if-else, nested if, if-elif-else ladder।
  5. if-elif-else ladder कैसे काम करती है?Conditions ऊपर से नीचे check होती हैं — जो पहली true मिली उसका block execute होकर बाकी सब skip; कोई true न हो तो else चलता है।
  6. Nested if किसे कहते हैं? कब उपयोग होता है?एक if के अंदर दूसरा if। जब multi-level checking करनी हो — जैसे पहले pass/fail check, फिर pass के अंदर distinction check।
  7. while और for loop में अंतर बताइए।while = condition-based, repetitions unknown, initialization/increment manually; for = sequence/range-based, repetitions known, सब range() संभालता है।
  8. break, continue और pass का अंतर उदाहरण सहित लिखिए।break loop को पूरा समाप्त करता है; continue current iteration skip करके अगले पर जाता है; pass कुछ नहीं करता (placeholder)। range(1,6) में i==3 पर continue से output 1 2 4 5।
  9. Infinite loop क्या है? कैसे बचें?ऐसा loop जो कभी नहीं रुकता — condition कभी false नहीं होती (counter बढ़ाना भूलने पर)। बचाव: loop में counter update व सही condition ज़रूर रखें।
  10. Nested loop क्या है? Total iterations कैसे निकालते हैं?Loop के अंदर loop। हर outer iteration पर inner पूरा चलता है — total = outer × inner (जैसे 3 × 4 = 12)।
  11. दो variables swap करने का algorithm लिखिए।Start → Input A, B → temp = A → A = B → B = temp → Print A, B → Stop। (Python shortcut: a, b = b, a)
  12. Number reverse करने की logic समझाइए।जब तक N > 0: N % 10 से आखिरी digit निकालो, rev = rev × 10 + digit से जोड़ो, N = N // 10 से digit हटाओ। 123 → 321।
  13. Palindrome number की जाँच कैसे होती है?Number की copy रखो, उसे reverse करो — अगर copy == reverse तो Palindrome (जैसे 121, 1331), वरना नहीं।
  14. Prime number check का algorithm लिखिए।Input N → अगर N < 2 तो prime नहीं → 2 से N-1 तक divide करके देखो → कहीं भी remainder 0 आया तो prime नहीं (break), वरना Prime।
  15. Leap year की condition समझाइए।(Year 4 से divisible AND 100 से नहीं) OR (400 से divisible)। इसलिए 2024 व 2000 leap हैं, पर 1900 नहीं (100 से divisible, 400 से नहीं)।
Revision Tip: Flowchart symbols की matching, break vs continue का output, और Factorial/Reverse/Palindrome के algorithms — ये 3 areas इस chapter से exam में सबसे ज़्यादा weightage रखते हैं। Section 2.7 के output questions दो बार solve करें!
FAQ

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

Algorithm और Flowchart में क्या अंतर है?
Algorithm problem हल करने के steps को text में लिखता है (language-independent, बनाना आसान); Flowchart उन्हीं steps को symbols व arrows के diagram में दिखाता है (देखते ही समझ आता है)। Algorithm = written plan, Flowchart = visual plan, Python code = implementation।
Flowchart के मुख्य symbols कौन-कौन से हैं?
Oval = Start/Stop (Terminal), Rectangle = Process (calculation), Parallelogram = Input/Output, Diamond = Decision (condition), Arrow = Flow lines, Circle = Connector, Document symbol = Print/Report।
Sequential, Decision और Iterative Processing क्या होती हैं?
Sequential में statements क्रम से ऊपर से नीचे चलते हैं (कोई branch/loop नहीं); Decision में condition के अनुसार रास्ता बदलता है (if-else); Iterative में steps बार-बार दोहराए जाते हैं जब तक condition true हो (while, for loops)।
Python में कितने types के loops होते हैं?
2 मुख्य loops — while loop (जब repetitions की संख्या पता न हो, condition-based) और for loop (जब पता हो, sequence/range-based)। एक loop के अंदर दूसरा loop = Nested loop। Control के लिए break, continue व pass statements हैं।
break, continue और pass में क्या अंतर है?
break loop को बीच में ही पूरी तरह समाप्त कर देता है; continue current iteration को skip करके अगले iteration पर चला जाता है; pass कुछ नहीं करता — केवल placeholder (empty statement) है।
O Level exam में Algorithm/Flowchart से कैसे questions आते हैं?
Symbol identification, flowchart completion, logic tracing (output बताना), और factorial/palindrome/prime जैसे programs के algorithm व flowchart बनाना — आमतौर पर 4-6 marks के descriptive व 2-3 marks के MCQs।

🎯 Chapter 2 पूरा हुआ! अब आगे बढ़ें

Chapter 3 में Python की असली शुरुआत — installation, variables, data types, input/output व पहला program।

Chapter 3 पढ़ें ➜