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 Construct | Condition Evaluation | Minimum Executions |
|---|---|---|
| while Loop | Pre-test (Entry controlled) | 0 times (if false initially) |
| for Loop | Pre-test (init; condition; update) | 0 times |
| do-while Loop | Post-test (Exit controlled) | 1 time guaranteed |
đŻ Learning Philosophy: Active Logic Building
Test your iterative logic across 3 Interactive Learning Modes:
- đ§ Tab 1 (Logic & Hints): Mathematical algorithms & loop invariants.
- đ§Š Tab 2 (Logic Puzzle): Complete missing loop bounds & accumulator statements!
- đť Tab 3 (C Solution): Verified C source code reference.
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 = 2up toi <= n / 2. - If
n % i == 0, a divisor is found →isPrime = false; break;
Interactive Logic Hints
đ§Š Complete Prime Check Loop Logic
for (i = 2; i n / 2; i++) {
if (n i == 0) {
isPrime = false;
;
}
}
đ˘ Live Prime Tester & Divisor Tracer
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
đ§Š Complete Binary to Decimal Conversion Loop
rem = bin 10;
dec += rem * pow(, i);
bin /= ;
i++;
}
đť Binary Positional Visualizer
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
temp down to 0! We need original num to compare num == rev!đ§Š Complete Reversal & Palindrome Logic
while (temp > 0) {
rem = temp % 10;
rev = rev * rem;
temp /= 10;
}
if (num rev) printf("Palindrome");
đ Reversal & Palindrome Simulator
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
dusingdigits++; temp /= 10; - Pass 2: Accumulate
sum += pow(digit, d)for each digit. - Check:
sum == num
Interactive Logic Hints
đ§Š Complete Armstrong Power Sum Logic
temp = num;
while (temp > 0) {
rem = temp % 10;
sum += (rem, );
temp /= 10;
}
⨠Armstrong Power Sum Visualizer
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
đ§Š Complete Sine Series Recurrence Logic
term = xrad; sum = term;
for (i = 1; i < n; i++) {
term = * xrad * xrad / ((2*i) * (2*i + 1));
sum += term;
}