Java Program to Print K Shape Number Pattern

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();
		}
	}
}
Java Program to Print K Shape Number Pattern 1

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 

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.