🧪
Lab 5 — Active Loops & Iteration Logic
Lab 5: Intro 🏠 Home
PSUC Lab 5 — Iterative Control Structures

Active Logic: Loops (while, for, do-while)

Master loop initialization, continuation conditions, counter updates, prime trial division, base conversions, digit reversal, and Taylor series accumulators.

Loop Constructs Comparison

Loop ConstructCondition EvaluationMinimum Executions
while LoopPre-test (Entry controlled)0 times (if false initially)
for LoopPre-test (init; condition; update)0 times
do-while LoopPost-test (Exit controlled)1 time guaranteed

🎯 Learning Philosophy: Active Logic Building

Test your iterative logic across 3 Interactive Learning Modes:

  1. 🧠 Tab 1 (Logic & Hints): Mathematical algorithms & loop invariants.
  2. 🧩 Tab 2 (Logic Puzzle): Complete missing loop bounds & accumulator statements!
  3. 💻 Tab 3 (C Solution): Verified C source code reference.
Solve loop puzzles before peeking at verified code solutions!
Program 1 of 5

Prime Number Check (Trial Division up to n/2)

Determines if an integer n > 1 has any divisor using a trial division loop running from i = 2 up to i <= n / 2.

Prime Division Algorithm

  • Base Check: If n <= 1, the number is NOT prime.
  • Test divisors from i = 2 up to i <= n / 2.
  • If n % i == 0, a divisor is found → isPrime = false; break;

Interactive Logic Hints

💡 Hint 1: Why break out of the loop immediately upon finding a divisor? ▼
Once a single divisor is found (e.g. 2 divides 10), we know 10 cannot be prime! Continuing the loop would waste CPU cycles!

🧩 Complete Prime Check Loop Logic

bool isPrime = true;
for (i = 2; i n / 2; i++) {
  if (n i == 0) {
    isPrime = false;
    ;
  }
}
Select loop conditions above and click Check!

🔢 Live Prime Tester & Divisor Tracer

Result will appear here...
prime_check.cReference C Code
#include <stdio.h> #include <stdbool.h> int main() { int n, i; bool isPrime = true; printf("Enter number: "); scanf("%d", &n); if (n <= 1) isPrime = false; else { for (i = 2; i <= n / 2; i++) { if (n % i == 0) { isPrime = false; break; } } } if (isPrime) printf("%d IS a Prime Number!\n", n); else printf("%d is NOT a Prime Number.\n", n); return 0; }
Program 2 of 5

Binary to Decimal Conversion

Converts a binary number into decimal using positional weights of powers of 2 (digit × 2i).

Bit Accumulator Logic

  • Extract last bit: rem = bin % 10
  • Accumulate: dec += rem * pow(2, i)
  • Drop last bit: bin /= 10
  • Increment power index: i++

Interactive Logic Hints

💡 Hint 1: Why treat binary input as decimal 10s initially? ▼
Using % 10 peels off 0s and 1s right-to-left, allowing us to multiply each bit by 2^i!

🧩 Complete Binary to Decimal Conversion Loop

while (bin != 0) {
  rem = bin 10;
  dec += rem * pow(, i);
  bin /= ;
  i++;
}
Select statements above and click Check!

💻 Binary Positional Visualizer

Result will appear here...
bin_to_dec.cReference C Code
#include <stdio.h> #include <math.h> int main() { long long bin; int dec = 0, rem, i = 0; printf("Enter binary number: "); scanf("%lld", &bin); while (bin != 0) { rem = bin % 10; dec += rem * pow(2, i); bin /= 10; i++; } printf("Decimal Value = %d\n", dec); return 0; }
Program 3 of 5

Reverse Number & Palindrome Check

Reverses integer digits using rev = rev * 10 + rem and compares reversed number with original.

Reversal Accumulator Logic

  • rem = temp % 10 (Extract digit)
  • rev = rev * 10 + rem (Shift digits left & add new digit)
  • temp /= 10 (Drop last digit)
  • Palindrome check: num == rev

Interactive Logic Hints

💡 Hint 1: Why must we save num in temp before the loop? ▼
Because the loop destroys temp down to 0! We need original num to compare num == rev!

🧩 Complete Reversal & Palindrome Logic

temp = num;
while (temp > 0) {
  rem = temp % 10;
  rev = rev * rem;
  temp /= 10;
}
if (num rev) printf("Palindrome");
Select statements above and click Check!

🔄 Reversal & Palindrome Simulator

Result will appear here...
reverse_num.cReference C Code
#include <stdio.h> int main() { int num, temp, rev = 0, rem; printf("Enter number: "); scanf("%d", &num); temp = num; while (temp > 0) { rem = temp % 10; rev = rev * 10 + rem; temp /= 10; } printf("Reversed = %d\n", rev); if (num == rev) printf("It IS a Palindrome!\n"); else printf("NOT a Palindrome.\n"); return 0; }
Program 4 of 5

Armstrong Number Verification

An n-digit number is an Armstrong number if the sum of its digits raised to the power n equals the number itself (for example: 153 = 13 + 53 + 33).

2-Pass Loop Algorithm

  • Pass 1: Count total digits d using digits++; temp /= 10;
  • Pass 2: Accumulate sum += pow(digit, d) for each digit.
  • Check: sum == num

Interactive Logic Hints

💡 Hint 1: What are classic 3-digit Armstrong numbers? ▼
153, 370, 371, 407! (e.g. 3^3 + 7^3 + 0^3 = 27 + 343 + 0 = 370).

🧩 Complete Armstrong Power Sum Logic

while (temp > 0) { digits++; temp /= 10; }

temp = num;
while (temp > 0) {
  rem = temp % 10;
  sum += (rem, );
  temp /= 10;
}
Select functions above and click Check!

✨ Armstrong Power Sum Visualizer

Result will appear here...
armstrong.cReference C Code
#include <stdio.h> #include <math.h> int main() { int num, temp, digits = 0, rem, sum = 0; printf("Enter number: "); scanf("%d", &num); temp = num; while (temp > 0) { digits++; temp /= 10; } temp = num; while (temp > 0) { rem = temp % 10; sum += pow(rem, digits); temp /= 10; } if (sum == num) printf("%d IS an Armstrong Number!\n", num); else printf("%d is NOT an Armstrong Number.\n", num); return 0; }
Program 5 of 5

Sum of Sine Series sin(x)

Computes Taylor expansion sin(x) = x - x3/3! + x5/5! - … up to N terms using term recurrence term = -term * x*x / ((2i)*(2i+1)).

Sine Recurrence Formula

  • Convert degrees to radians: xrad = xdeg * (3.14159 / 180.0)
  • First term: term = xrad; sum = term;
  • Next term recurrence: term = -term * xrad * xrad / ((2*i)*(2*i+1))

Interactive Logic Hints

💡 Hint 1: Why use term recurrence instead of calculating factorials separately? ▼
Factorials like 15! overflow integer limits! Recurrence updating avoids factorial overflow completely!

🧩 Complete Sine Series Recurrence Logic

xrad = xdeg * (3.14159 / );
term = xrad; sum = term;
for (i = 1; i < n; i++) {
  term = * xrad * xrad / ((2*i) * (2*i + 1));
  sum += term;
}
Select recurrence factors above and click Check!

📈 Sine Taylor Series Visualizer

Series sum result...
sin_series.cReference C Code
#include <stdio.h> #include <math.h> int main() { int n, i; float xdeg, xrad, sum = 0, term; printf("Enter x in degrees & terms N: "); scanf("%f %d", &xdeg, &n); xrad = xdeg * (3.14159 / 180.0); term = xrad; sum = term; for (i = 1; i < n; i++) { term = -term * xrad * xrad / ((2*i) * (2*i + 1)); sum += term; } printf("sin(%.2f°) = %.6f\n", xdeg, sum); return 0; }
Knowledge Check

Test Your Loop Logic & Series Mastery

Question 1 of 5
Loading question...