KwickAcademy Java · 6 min · free
Nested Loops, Patterns and Number Programs
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 i | j values | Printed |
|---|---|---|
| i = 1 | j = 1, 2, 3 | (1,1) (1,2) (1,3) |
| i = 2 | j = 1, 2, 3 | (2,1) (2,2) (2,3) |
| i = 2 → 3 | j restarts at 1 | new row |
Special numbers
| Number | Rule | Example |
|---|---|---|
| Prime | exactly 2 factors | 7: 1 and 7 |
| Perfect | factors sum to it | 6 = 1+2+3 |
| Armstrong | digit cubes sum | 153 = 1+125+27 |
| Palindrome | same reversed | 121 |
Dry run: a trace table
| i | j runs | Row prints |
|---|---|---|
| 1 | 3 | 3 |
| 2 | 3, 2 | 32 |
| 3 | 3, 2, 1 | 321 |
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.
How many prints does a 3 by 3 nested loop make?43 sec
Why does each row get one more star?40 sec
Why is 153 special?41 sec
What is a dry run?42 secThe 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
| Course | Unit |
|---|---|
| ICSE Class 9 Computer Applications | Nested for Loops |
| ICSE Class 10 Computer Applications | String Handling |
| Programming All levels Java | Control 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.

