PSUC Lab 6 — Multidimensional Iterations
Active Logic: Nested Loops (Outer & Inner)
Master 2D loop mechanics for pattern generation (Floyd's triangle, pyramids), multiplication table matrices, bounded Fibonacci ranges, and N-th Prime/Armstrong search.
Nested Loop Rules
| Concept | Outer Loop (i) | Inner Loop (j) |
|---|---|---|
| Role | Controls Rows / Steps | Controls Columns / Print per Row |
| Execution Count | Runs N times | Runs N * M total times! |
| Reset Behavior | Increments once per row | Re-initializes completely every outer iteration! |
🎯 Learning Philosophy: Active Logic Building
Test your nested loop logic across 3 Interactive Learning Modes:
- 🧠 Tab 1 (Logic & Hints): Row-column mapping & loop invariants.
- 🧩 Tab 2 (Logic Puzzle): Fill in missing outer/inner loop bounds & live pattern previewers!
- 💻 Tab 3 (C Solution): Reference verified C code solutions.
Complete pattern puzzles before peeking at C source code!
Exercise 1 of 5
Pattern Printing (Floyd's Triangle & Pyramids)
Pattern 1: Floyd's Triangle (1 to 15 sequence) | Pattern 2: Number Repeat Pyramid.
Pattern Bounds
- Outer loop
i = 1 to 5: controls row number - Inner loop
j = 1 to i: controls number of items printed in rowi - Floyd's Triangle uses persistent counter
num++
Interactive Logic Hints
💡 Hint 1: Why does inner loop run j <= i? ▼
Because row 1 needs 1 item, row 2 needs 2 items, row 3 needs 3 items... so inner loop limit is
j <= i!🧩 Complete Nested Pattern Loops
int num = 1;
for (i = 1; i <= 5; i++) {
for (j = 1; j ; j++) {
printf("%d ", num++);
}
printf("\n");
}
for (i = 1; i <= 5; i++) {
for (j = 1; j ; j++) {
printf("%d ", num++);
}
printf("\n");
}
Select inner loop bound above and click Check!
🎨 Live Pattern Generator
Pattern output...
pattern_floyd.cReference C Code
#include <stdio.h>
int main() {
int i, j, num = 1;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++) {
printf("%d ", num++);
}
printf("\n");
}
return 0;
}
Exercise 2 of 5
Multiplication Tables Matrix (N Numbers up to K Terms)
Generates multiplication tables for numbers 1 to N up to K terms using nested loops.
Matrix Table Logic
- Outer loop
i = 1 to N: table number - Inner loop
j = 1 to K: multiplier term - Cell value:
i * j
Interactive Logic Hints
💡 Hint 1: How does matrix formatting work? ▼
Use
printf("%4d", i * j) to format numbers cleanly into aligned columns!🧩 Complete Multiplication Table Loops
for (i = 1; i <= n; i++) {
for (j = 1; j <= k; j++) {
printf("%d\t", i j);
}
printf("\n");
}
for (j = 1; j <= k; j++) {
printf("%d\t", i j);
}
printf("\n");
}
Select operator above and click Check!
✖️ Multiplication Matrix Generator
Matrix output...
mult_table.cReference C Code
#include <stdio.h>
int main() {
int n = 3, k = 5, i, j;
for (i = 1; i <= n; i++) {
printf("Table of %d: ", i);
for (j = 1; j <= k; j++) {
printf("%d ", i * j);
}
printf("\n");
}
return 0;
}
Exercise 3 of 5
Fibonacci Numbers in Range [Min, Max]
Generates Fibonacci series numbers Fn = Fn-1 + Fn-2 and prints only those falling within [Min, Max].
Fibonacci Update Rule
- Initial:
a = 0, b = 1, c = a + b - Shift:
a = b; b = c; c = a + b; - Filter: Print
conly ifc >= min && c <= max
Interactive Logic Hints
💡 Hint 1: When should loop stop? ▼
Stop loop when next Fibonacci term
c > max!🧩 Complete Fibonacci Range Loop
int a = 0, b = 1, c = a + b;
while (c max) {
if (c >= min) printf("%d ", c);
a = b;
b = ;
c = a b;
}
while (c max) {
if (c >= min) printf("%d ", c);
a = b;
b = ;
c = a b;
}
Select statements above and click Check!
🌀 Fibonacci Range Generator
Fibonacci range...
fibo_range.cReference C Code
#include <stdio.h>
int main() {
int min = 10, max = 100;
int a = 0, b = 1, c = a + b;
printf("Fibonacci in [%d, %d]: ", min, max);
while (c <= max) {
if (c >= min) printf("%d ", c);
a = b; b = c; c = a + b;
}
printf("\n");
return 0;
}
Exercise 4 of 5
Find the N-th Prime Number
Uses nested loops to search numbers starting from 2, testing each for primality until the N-th prime is discovered.
Search Loop Logic
- Outer loop: test candidates
num = 2, 3, 4... - Inner loop: test prime divisors
i = 2 to num/2 - If prime,
count++. Whencount == N, stop!
Interactive Logic Hints
💡 Hint 1: What is the 10th Prime Number? ▼
The 10th prime number is 29! (2, 3, 5, 7, 11, 13, 17, 19, 23, 29).
🧩 Complete N-th Prime Search Loop
int count = 0, num = 2;
while (count N) {
bool isPrime = true;
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) { isPrime = false; break; }
}
if (isPrime) ;
if (count < N) num++;
}
while (count N) {
bool isPrime = true;
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) { isPrime = false; break; }
}
if (isPrime) ;
if (count < N) num++;
}
Select statements above and click Check!
🔢 N-th Prime Discovery Generator
Prime output...
nth_prime.cReference C Code
#include <stdio.h>
#include <stdbool.h>
int main() {
int targetN = 10, count = 0, num = 2, i;
while (count < targetN) {
bool isPrime = true;
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) { isPrime = false; break; }
}
if (isPrime) count++;
if (count < targetN) num++;
}
printf("The %d-th Prime Number is %d\n", targetN, num);
return 0;
}
Exercise 5 of 5
Find the N-th Armstrong Number
Uses nested loops to search numbers starting from 1, checking power sums for each until the N-th Armstrong number is found.
Armstrong Sequence
- Single digits (1-9) are Armstrong numbers (d1 = d).
- 10th Armstrong number = 153 (13 + 53 + 33 = 153).
- 11th Armstrong number = 370 (33 + 73 + 03 = 370).
Interactive Logic Hints
💡 Hint 1: How many Armstrong numbers exist? ▼
There are only 88 Armstrong numbers in base 10! The largest has 39 digits!
🧩 Complete N-th Armstrong Search Loop
while (count < targetN) {
int d = countDigits(num);
if (sumPowerDigits(num, d) num) {
count++;
}
if (count < targetN) num++;
}
int d = countDigits(num);
if (sumPowerDigits(num, d) num) {
count++;
}
if (count < targetN) num++;
}
Select comparison operator above and click Check!
✨ N-th Armstrong Discovery Generator
Armstrong output...
nth_armstrong.cReference C Code
#include <stdio.h>
#include <math.h>
int main() {
int targetN = 10, count = 0, num = 1, temp, d, sum, rem;
while (count < targetN) {
d = 0; temp = num;
while (temp > 0) { d++; temp /= 10; }
sum = 0; temp = num;
while (temp > 0) {
rem = temp % 10;
sum += pow(rem, d);
temp /= 10;
}
if (sum == num) count++;
if (count < targetN) num++;
}
printf("The %d-th Armstrong Number is %d\n", targetN, num);
return 0;
}
Knowledge Check
Test Your Nested Loops Mastery
Question 1 of 5
Loading question...