Write a Java program to print K shape number pattern using a for loop with an example.
import java.util.Scanner; public class KshapeNumber1 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int i, j; System.out.print("Enter K Shape Number Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing K Shape Numbers Pattern"); for (i = rows; i >= 1; i-- ) { for (j = 1 ; j <= i; j++ ) { System.out.print(j+ " "); } System.out.println(); } for (i = 2 ; i <= rows; i++ ) { for (j = 1 ; j <= i; j++ ) { System.out.print(j+ " "); } System.out.println(); } } }
This Java Program displays the K Shape Pattern of Numbers using a while loop.
import java.util.Scanner; public class KshapeNumber2 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Rows = "); int rows = sc.nextInt(); int i = rows, j; while (i >= 1 ) { j = 1 ; while (j <= i ) { System.out.print(j+ " "); j++; } System.out.println(); i--; } i = 2 ; while( i <= rows ) { j = 1 ; while (j <= i ) { System.out.print(j+ " "); j++; } System.out.println(); i++; } } }
Enter Rows = 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
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
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
Java Program to Print K Shape Number Pattern using do while loop.
import java.util.Scanner; public class KshapeNumber3 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Rows = "); int rows = sc.nextInt(); int i = rows, j; do { j = 1 ; do { System.out.print(j+ " "); } while (++j <= i ); System.out.println(); } while (--i >= 1 ); i = 2 ; do { j = 1 ; do { System.out.print(j+ " "); } while (++j <= i ); System.out.println(); } while( ++i <= rows ); } }
Enter Rows = 12
1 2 3 4 5 6 7 8 9 10 11 12
1 2 3 4 5 6 7 8 9 10 11
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
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
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10 11
1 2 3 4 5 6 7 8 9 10 11 12