Print Number Pattern - 5 | Java Programs

Output:

Enter the number of rows: 5

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

Click Here For Java Online Compiler

Solution:

import java.util.Scanner;

class NumberPattern5 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the number of rows: ");
        int base = scanner.nextInt();
        System.out.println();
        for (int i = base; i > 0; i--) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
        for (int i = 2; i <= base; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}
Share This :

Related Post



sentiment_satisfied Emoticon