1. Iterative Control Structures: for, while, and do-while

Iterative control structures (loops), mathematical series summation, and number theory algorithms form the backbone of Unit 2 in MAKAUT C Programming (PPS-II). In university end-semester papers, Unit 2 accounts for over 25 to 30 marks, including mandatory pattern-printing questions in Group B and multi-part numerical algorithms in Group C.

This comprehensive guide covers loops, jump statements, series evaluation formulas, and 40+ solved programs straight from unit_2_loops_series_number_patterns.

---

1. Comparative Analysis of C Loops

A loop repeatedly executes a block of code until a specified test condition evaluates to false (0).

Feature`for` loop`while` loop`do-while` loop
Control TypeEntry-ControlledEntry-ControlledExit-Controlled
Minimum Executions0 times0 timesAt least 1 time
Best ForKnown number of iterationsCondition-driven iterationsMenu-driven interactive programs
Syntaxfor(init; cond; upd)while(cond)do { ... } while(cond);
NOTE

In do-while(condition);, the trailing semicolon is mandatory. Omitting it triggers a syntax error.

---

2. Jump Statements: break, continue, goto

  • `break`: Exits the immediate loop or switch block unconditionally.
  • `continue`: Skips the remaining code in the current iteration and jumps directly to the loop update step.
  • `goto`: Performs an unconditional jump to a labeled statement (label:). Discouraged in modern software development as it breaks structured programming principles.

---

3. Mathematical Series Summation Algorithms

In MAKAUT exams, series summation questions test your ability to translate mathematical summation formulas (\sum) into algorithmic accumulator variables (sum += term).

Series 1: \sum_{i=1}^{n} x^i = x + x^2 + x^3 + \dots + x^n

c

---

Series 2: Harmonic Fractional Series \sum_{i=1}^{n} 1i = 1 + 12 + 13 + \dots + 1n

c

---

4. Solved Core Number Algorithms

1. Prime Number Check (Optimized O(\sqrt{n}) Time)

*Exam Context: Group B (5 Marks)*

c

---

2. Armstrong Number Check in a Given Range

An n-digit number is an Armstrong number if the sum of its digits raised to the n-th power equals the original number (e.g., 153 = 1^3 + 5^3 + 3^3).

c

---

3. Krishnamurthy (Strong) Number Verification

A number is a Krishnamurthy number if the sum of the factorials of its digits equals the number itself (e.g., 145 = 1! + 4! + 5! = 1 + 24 + 120 = 145).

c

---

4. Euclidean Algorithm for Greatest Common Divisor (GCD)

*Exam Context: Group B (5 Marks)*

c

---

5. Pattern Printing Mechanics

Pattern printing requires nested loop coordination:

  • Outer Loop (`i`): Controls row count.
  • Inner Loop 1: Prints leading space padding per row.
  • Inner Loop 2 (`j`): Prints characters/numbers per row.

Pyramid Star Pattern

c
c

---

6. MAKAUT Revision Summary

  • Always double check loop bounds (i < n vs i \le n) to avoid off-by-one errors.
  • For integer digit extractions, use rem = num % 10 followed by num /= 10.
  • In series problems, typecast division operands to float or double (1.0 / i) to prevent zeroing out integer division.