KnowledgeBoat Logo

Computer Applications

Write the code to print following patterns

(i)

1
2 6
3 7 10
4 8 11 13
5 9 12 14 15

(ii)

A A A A A 
A A A B B
A A C C C 
A D D D D 
E E E E E

Java

Java Nested for Loops

6 Likes

Answer

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

Output

BlueJ output of Write the code to print following patterns (i) 1 2 6 3 7 10 4 8 11 13 5 9 12 14 15 (ii) A A A A A A A A B B A A C C C A D D D D E E E E E

Answered By

5 Likes


Related Questions