🔢 Control Structures

Nested Loops & Number Programs

⏱ 4 hr3 topicsInteractive
🎯 By the end: You can use a loop inside a loop to print patterns, sum series, and write the standard number-checking programs.

Putting one loop inside another — a nested loop — lets you work with grids, patterns and tables. Combined with the number tricks here, these are the most common ICSE Class 9 programs.

1Nested loops & patterns

A nested loop is a loop inside another loop. The outer loop usually controls the rows and the inner loop the columns:

// a right-angled triangle of stars
for (int i = 1; i <= 4; i++) {        // rows
  for (int j = 1; j <= i; j++)        // columns (grows each row)
    System.out.print("* ");
  System.out.println();               // newline after each row
}

Output:

* 
* * 
* * * 
* * * * 
For each pass of the outer loop, the inner loop runs fully. Change the inner loop's limit (fixed vs i) to switch between rectangles and triangles.
Key points
  • A nested loop is a loop inside a loop; the outer controls rows, the inner controls columns.
  • For each outer pass, the inner loop runs completely.
  • A fixed inner limit gives a rectangle; an inner limit of i gives a triangle.

2Mathematical series

A loop can build a series — adding or multiplying a chain of terms with a single variable:

// Sum: 1 + 2 + 3 + ... + n
int sum = 0;
for (int i = 1; i <= n; i++)
  sum = sum + i;

// Sum of squares: 1² + 2² + ... + n²
int s = 0;
for (int i = 1; i <= n; i++)
  s = s + i * i;
The pattern is always the same: start an accumulator at 0 (for sums) or 1 (for products), then update it inside the loop with each term.
Key points
  • A loop builds a series by updating an accumulator with each term.
  • Start the accumulator at 0 for a sum, 1 for a product.
  • Work out the general term (i, i*i, etc.) and add/multiply it each pass.

3Classic number programs

These come up again and again in exams. Each uses a loop and a check:

ProgramA number is … if
PrimeIt has exactly two factors: 1 and itself
CompositeIt has more than two factors (not prime)
PerfectThe sum of its factors (excluding itself) equals the number (6 = 1+2+3)
ArmstrongThe sum of each digit cubed equals the number (153 = 1³+5³+3³)
PalindromeIt reads the same reversed (121, 1331)
FibonacciSeries where each term is the sum of the previous two (0,1,1,2,3,5,8…)
// count factors to test for prime
int count = 0;
for (int i = 1; i <= n; i++)
  if (n % i == 0) count++;
if (count == 2) System.out.println("Prime");
Two key techniques recur: finding factors with n % i == 0, and breaking a number into digits with n % 10 (last digit) and n / 10 (drop the last digit).
Key points
  • Prime = exactly two factors; composite = more than two; perfect = factor-sum (excluding itself) equals the number.
  • Armstrong = sum of digit-cubes equals the number; palindrome = same reversed; Fibonacci = each term is the sum of the previous two.
  • Core tricks: factors via n % i == 0; digits via n % 10 (last digit) and n / 10 (remove it).

★ Practical: patterns & numbers

In BlueJ, write programs that:

  1. Print a 4×4 rectangle of stars, then a right-angled triangle of stars.
  2. Find the sum 1 + 2 + 3 + ... + n for an entered n.
  3. Check whether an entered number is prime (count its factors).
  4. Check whether an entered number is a palindrome (reverse it and compare).

Ready for the chapter test?

Answer 5 questions. Score 60% or more to mark this chapter complete.

Start the test →

💡 Log in to save your progress and earn the certificate.