CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Java · 6 min · free

Nested Loops, Patterns and Number Programs

6 min4 KwickClipsFull text belowFree
Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

How nested loops fill a grid row by row, star, number and letter patterns, the special number programs, and dry running output by hand.

Follows the syllabus of: ICSE Class 9 Computer Applications, ICSE Class 10 Computer Applications

On screen in this lesson

What is a nested loop?

A loop written inside the body of another loop
Outer loop: usually the rows
Inner loop: usually the columns
Inner loop runs fully for every outer iteration

The grid: i rows, j columns

Row ij valuesPrinted
i = 1j = 1, 2, 3(1,1) (1,2) (1,3)
i = 2j = 1, 2, 3(2,1) (2,2) (2,3)
i = 2 → 3j restarts at 1new row

Special numbers

NumberRuleExample
Primeexactly 2 factors7: 1 and 7
Perfectfactors sum to it6 = 1+2+3
Armstrongdigit cubes sum153 = 1+125+27
Palindromesame reversed121

Dry run: a trace table

ij runsRow prints
133
23, 232
33, 2, 1321

Recap

Inner loop runs fully for every outer iteration
Outer loop: rows; inner loop: items in a row
print stays on the line; println() starts a new one
% 10 gives the last digit; / 10 removes it
Dry run with a trace table of i and j

Quick answers

Why does each row of the triangle get one more star?

The inner loop runs up to i, the row number.

How do you pull the digits out of a number?

% 10 gives the last digit and / 10 removes it.

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. How does Java print a triangle of stars, row by row? The secret is a loop inside a loop. Today we see how nested loops run, print number, letter and star patterns, and write the special number programs. We will also learn to dry run the output, just like in the exam.

A nested loop is a loop written inside the body of another loop. Think of a clock, where the minute hand goes round fully for every single hour. In patterns, the outer loop usually controls the rows. The inner loop usually controls the columns, or the items in one row. For every one iteration of the outer loop, the inner loop runs completely.

Let us watch the grid, with i for rows and j for columns, both going up to three. When i is one, j goes one, two, three, so row one fills from left to right. When i is two, j starts again from one, and row two fills. Each time i moves to a new row, j restarts at one, and we print a new line.

Here is a small multiplication grid. The inner loop prints i times j with print, which stays on the same line. After the inner loop ends, println with empty brackets moves to a new line. So row one prints one, two, three, and row two prints two, four, six. The inner print ran nine times in total, three times three.

Here is the triangle from our question. The key change is in the inner loop, where j goes up to i, not to a fixed number. So row one prints one star, row two prints two stars, and so on. The rule for any pattern is simple. Find how many items each row has, in terms of the row number i.

Pause and predict. This is the same loop, but it prints j instead of a star. What does row three show? It shows one two three, because j counts from one up to three. If we printed i instead, row three would show three three three.

Letters can be counted too, because every char has a number code. Capital A is sixty five, and B is sixty six. Here the inner loop uses a char c, starting at A. It keeps going while c is less than A plus i. So row one prints A, and row four prints A, B, C and D.

Now the special number programs. A factor is a number that divides another number with no remainder. A prime number has exactly two factors, one and itself, like seven. A perfect number equals the sum of its factors other than itself, like six, which is one plus two plus three. A three digit Armstrong number equals the sum of the cubes of its digits, like one hundred fifty three. A palindrome number reads the same when reversed, like one two one.

To check a prime, we count its factors. The percent sign is the remainder operator, so n percent i equals zero means i divides n exactly. For seven, only one and seven divide it, so count becomes two. Count equals two is true, so seven is prime. For a perfect number, we would add the factors below n instead of counting them.

Armstrong and palindrome programs both take a number apart, digit by digit. Here t starts as one hundred fifty three. T percent ten gives the last digit d, and t divided by ten removes it. Sum adds the cube of each digit, and rev builds the reversed number. Sum is one hundred fifty three, equal to the number, so it is Armstrong. Rev is three five one, which is different, so it is not a palindrome. In a full program, keep a copy of the number to compare, because t ends at zero.

A dry run means running the code by hand, on paper, before the computer does. Take a loop where j starts at three and counts down, i times. When i is one, j takes only three, so the row prints three. When i is two, j takes three and two, so the row prints three two. When i is three, j takes three, two and one, so the row prints three two one.

Here is that code. Pause, and write your own trace table first. When i is one, j starts at three, and runs while j is greater than two. So only three prints. When i is three, j runs while j is greater than zero, so three, two, one prints. Check your answer against the output.

Let us recap. The inner loop runs completely for every iteration of the outer loop. In patterns, the outer loop is the rows, and the inner loop is the items in a row. Print stays on the same line, and println with empty brackets starts a new one. For number programs, percent ten gives the last digit, and divided by ten removes it. And always dry run with a trace table of i and j.

Courses that teach this

CourseUnit
ICSE Class 9 Computer ApplicationsNested for Loops
ICSE Class 10 Computer ApplicationsString Handling
Programming All levels JavaControl Flow and Iteration

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →