Pattern Programs in Java for ICSE Class 9: 8 Solved Patterns with Explanations
ICSE Class 9 Computer Applications pattern programs in Java using nested loops: number and star triangles, inverted triangles, Floyd's triangle, a pyramid, letter and word patterns and a 0-1 pattern, each with output and the logic explained.

Pattern programs are the best way to master nested loops, and they appear regularly in ICSE Class 9 tests and in Class 10 Section B. All of them use one idea: the outer loop controls the rows, and the inner loop controls what is printed in each row.
The rule for every pattern
for (int i = 1; i <= n; i++) { // one pass per ROW
for (int j = 1; j <= ?; j++) // items in this row
System.out.print(? + " ");
System.out.println(); // move to the next row
}Answer two questions for every pattern: how many items does row i have, and what is printed at position j?
Pattern 1: number triangle
1
1 2
1 2 3
1 2 3 4for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++)
System.out.print(j + " ");
System.out.println();
}Row i has i items, and position j prints j.
Pattern 2: repeated row number
1
2 2
3 3 3
4 4 4 4Same loops, but print i instead of j: System.out.print(i + " ");
Pattern 3: inverted triangle
1 2 3 4
1 2 3
1 2
1for (int i = 4; i >= 1; i--) {
for (int j = 1; j <= i; j++)
System.out.print(j + " ");
System.out.println();
}The outer loop counts down, so each row is shorter than the one before.
Pattern 4: Floyd's triangle
1
2 3
4 5 6
7 8 9 10int k = 1;
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(k + " ");
k++;
}
System.out.println();
}A separate counter k keeps increasing across the rows.
Pattern 5: star pyramid
*
* *
* * *
* * * *int n = 4;
for (int i = 1; i <= n; i++) {
for (int s = 1; s <= n - i; s++) // leading spaces
System.out.print(" ");
for (int j = 1; j <= i; j++) // stars
System.out.print("* ");
System.out.println();
}Row i starts with n − i spaces, then prints i stars, each followed by a space.
Pattern 6: letter triangle
A
A B
A B C
A B C Dfor (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++)
System.out.print((char) ('A' + j - 1) + " ");
System.out.println();
}'A' + 0 is A, 'A' + 1 is B, and so on. The cast (char) turns the code back into a letter.
Pattern 7: 0-1 triangle
1
0 1
1 0 1
0 1 0 1for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++)
System.out.print(((i + j) % 2 == 0 ? 1 : 0) + " ");
System.out.println();
}When i + j is even, print 1; when it is odd, print 0.
Pattern 8: word pattern
J
J A
J A V
J A V AString s = "JAVA";
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j <= i; j++)
System.out.print(s.charAt(j) + " ");
System.out.println();
}Common mistakes
- Using
printlninside the inner loop, which prints every item on a new line. - Forgetting the
System.out.println()after the inner loop, which puts the whole pattern on one line. - In pyramids, getting the number of spaces wrong. Write out row 1 and the last row on paper first.
- Not resetting a counter that should restart in each row, or resetting one (like Floyd's
k) that shouldn't.
Practice
Write programs for these patterns:
(a) * * * * (b) 5 4 3 2 1 (c) 1
* * * 5 4 3 2 2 4
* * 5 4 3 3 6 9
* 5 4 4 8 12 16Hints:
- (a) is Pattern 3 with stars.
- (b) is an inner loop from 5 down to 6 − i.
- (c) prints i × j.
Next, try Series Programs in Java and test yourself with the ICSE Class 9 Computer Applications Sample Paper.
Related Posts
Continue learning with these related articles
Sum of First n Natural Numbers in Java (Loop and Formula)
Find the sum of the first n natural numbers in Java with a for loop, a while loop and the formula n(n + 1)/2, with a dry run and the sums of even numbers, odd numbers, squares and cubes.
Prime Number Program in Java: Check a Number and Print Primes in a Range
Check whether a number is prime in Java by counting its factors, a faster square-root method with isPrime(), and a program that prints all prime numbers in a range.

Encapsulation, Access Specifiers and Scope of Variables in Java for ICSE Class 10
ICSE Class 10 Computer Applications notes on encapsulation: private, default, protected and public access, data hiding with getter and setter methods, instance vs static (class) variables, local variables and scope, with a solved BankAccount program.