KnowledgeBoat Logo
|

Computer Applications

Write a program to print the following patterns.

(i)

5
4 5
3 4 5
2 3 4 5 
1 2 3 4 5
    

(ii)

J
I H
G F E
D C B A

Java

Java Nested for Loops

9 Likes

Answer

public class KboatPattern
{
    public static void main(String args[]) {
        
        System.out.println("Pattern 1 ");
        for (int k = 5; k >= 1; k--) {
            for (int l = k; l <= 5; l++) {
                System.out.print(l + " ");
            }
            System.out.println();
        }
        
        System.out.println();
        
        System.out.println("Pattern 2 ");
        char ch = 'J';
        for (int i = 1; i <= 4; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(ch + " ");
                ch--;
            }
            System.out.println();
        }
        
    }
}

Output

BlueJ output of Write a program to print the following patterns. (i) 5 4 5 3 4 5 2 3 4 5 1 2 3 4 5 (ii) J I H G F E D C B A

Answered By

5 Likes


Related Questions