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 Type | Entry-Controlled | Entry-Controlled | Exit-Controlled |
| Minimum Executions | 0 times | 0 times | At least 1 time |
| Best For | Known number of iterations | Condition-driven iterations | Menu-driven interactive programs |
| Syntax | for(init; cond; upd) | while(cond) | do { ... } while(cond); |
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
switchblock 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
1"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>2"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><math.h>34int main() {5 int x, n;6 double sum = 0.0;78 printf("Enter base(x) and limit(n): ");9 scanf("%d %d", &x, &n);1011 for (int i = 1; i <= n; i++) {12 sum += pow(x, i);13 }1415 printf("Sum of series = %.2lf\n", sum);16 return 0;17}
---
Series 2: Harmonic Fractional Series \sum_{i=1}^{n} 1i = 1 + 12 + 13 + \dots + 1n
1"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>23int main() {4 int n;5 double sum = 0.0;67 printf("Enter n: ");8 scanf("%d", &n);910 for (int i = 1; i <= n; i++) {11 sum += 1.0 / i; // 1.0 ensures floating point division!12 }1314 printf("Harmonic Series Sum = %.4lf\n", sum);15 return 0;16}
---
4. Solved Core Number Algorithms
1. Prime Number Check (Optimized O(\sqrt{n}) Time)
*Exam Context: Group B (5 Marks)*
1"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>2"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdbool.h>34bool isPrime(int n) {5 if (n <= 1) return false;6 for (int i = 2; i * i <= n; i++) {7 if (n % i == 0) return false;8 }9 return true;10}1112int main() {13 int num;14 printf("Enter number: ");15 scanf("%d", &num);1617 if (isPrime(num))18 printf("%d is a Prime Number.\n", num);19 else20 printf("%d is NOT a Prime Number.\n", num);2122 return 0;23}
---
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).
1"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>2"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><math.h>34int main() {5 int low, high;6 printf("Enter range(low high): ");7 scanf("%d %d", &low, &high);89 printf("Armstrong numbers between %d and %d:\n", low, high);10 for (int i = low; i <= high; i++) {11 int temp = i, count = 0, sum = 0;1213 // Step 1: Count number of digits14 int t = i;15 while (t > 0) { count++; t /= 10; }1617 // Step 2: Sum digit powers18 t = i;19 while (t > 0) {20 int rem = t % 10;21 sum += (int)pow(rem, count);22 t /= 10;23 }2425 if (sum == i) {26 printf("%d ", i);27 }28 }29 printf("\n");30 return 0;31}
---
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).
1"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>23int factorial(int n) {4 int fact = 1;5 for (int i = 1; i <= n; i++) fact *= i;6 return fact;7}89int main() {10 int num, temp, sum = 0;11 printf("Enter integer: ");12 scanf("%d", &num);1314 temp = num;15 while (temp > 0) {16 int rem = temp % 10;17 sum += factorial(rem);18 temp /= 10;19 }2021 if (sum == num)22 printf("%d is a Krishnamurthy Number.\n", num);23 else24 printf("%d is NOT a Krishnamurthy Number.\n", num);2526 return 0;27}
---
4. Euclidean Algorithm for Greatest Common Divisor (GCD)
*Exam Context: Group B (5 Marks)*
1"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>23int main() {4 int a, b;5 printf("Enter two numbers: ");6 scanf("%d %d", &a, &b);78 int origA = a, origB = b;9 while (b != 0) {10 int temp = b;11 b = a % b;12 a = temp;13 }1415 printf("GCD of %d and %d = %d\n", origA, origB, a);16 return 0;17}
---
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
1*2 ***3 *****4 *******5*********
1"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>23int main() {4 int rows;5 printf("Enter number of rows: ");6 scanf("%d", &rows);78 for (int i = 1; i <= rows; i++) {9 // Outer loop controls rows10 for (int space = 1; space <= rows - i; space++) {11 printf(" ");12 }13 for (int j = 1; j <= (2 * i - 1); j++) {14 printf("*");15 }16 printf("\n");17 }18 return 0;19}
---
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 % 10followed bynum /= 10. - In series problems, typecast division operands to
floatordouble(1.0 / i) to prevent zeroing out integer division.