Print Number Pattern - 3 | Java Programs

Output:

Enter the number of rows: 8

1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1 
1 2 3 4 5 6 5 4 3 2 1 
1 2 3 4 5 6 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 

Click Here For Java Online Compiler

Solution:

import java.util.Scanner;

class NumberPattern3 {

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

Related Post



sentiment_satisfied Emoticon