pyramid number pattern in Java using for loop
In this tutorial, we will dicuss pyramid number pattern in Java using for loop
Pyramid Pattern 1

Program
import java.util.Scanner; public class Pyramid_pattern1{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); //Scanner class in java System.out.print("Enter the rows you want:"); int rows=sc.nextInt(); System.out.println(""); for (int i=1; i<=rows; i++){//outer forloop for (int j=1; j<=(rows-i)*2; j++){ System.out.print(" ");//create initial space for pyramid shape } for (int k=i; k>=1; k--){//inner for loops System.out.print(" "+k);//create left half } for (int l=2; l<=i; l++){ System.out.print(" "+l); //create right half }//end outer for loop System.out.println(); } } }
When the above code is executed, it produces the following results:
option 1
Enter the rows you want: 5 1 2 1 2 3 2 1 2 3 4 3 2 1 2 3 4 5 4 3 2 1 2 3 4 5
option 2
Enter the rows you want: 9 1 2 1 2 3 2 1 2 3 4 3 2 1 2 3 4 5 4 3 2 1 2 3 4 5 6 5 4 3 2 1 2 3 4 5 6 7 6 5 4 3 2 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
Pyramid Pattern 2
Program
import java.util.Scanner; public class Pyramid_pattern2{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); //Scanner class in java System.out.print("Enter the rows you want: "); int rows=sc.nextInt();//get input from user System.out.println(""); for(int i=1; i<=rows; i++){ for(int j=0; j<rows-i; j++){//create initial space for pyramid shape System.out.print(" "); } for(int k=1; k<i; k++){ System.out.print(k);//create right half } for(int l=i; l>=1; l--){ System.out.print(l);//create left half } System.out.print("\n"); } } }
When the above code is executed, it produces the following results:
Option 1
Enter your rows you want :5 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
Option 2
Enter the rows you want: 9 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 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
Pyramid Pattern 3
Program
import java.util.Scanner; public class Pyramid_pattern3{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); //Scanner class in java System.out.print("Enter the rows you want"); int rows=sc.nextInt(); System.out.println(""); for(int i=1; i<=rows; i++){ for(int j=0; j<rows-i; j++){ System.out.print(" "); } for(int k=1; k<=i; k++){ System.out.print(k+" "); } System.out.print("\n"); } } }
When the above code is executed, it produces the following results:
Option 1
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Option 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
Pyramid Pattern 4
Program
import java.util.Scanner; public class Pyramid_pattern4{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); //Scanner class in java System.out.print("Enter the rows you want"); int rows=sc.nextInt(); System.out.println(""); for (int i=1; i<=rows; i++){ for (int j=1; j<=rows; j++){ System.out.print(" "); } rows--; for (int k=1; k<=i; k++){ System.out.print(i+" "); } System.out.println(); } } }
When the above code is executed, it produces the following results:
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
Pyramid Pattern 5
Program
import java.util.Scanner; public class Pyramid_pattern5{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); //Scanner class in java System.out.print("Enter the rows you want"); int rows=sc.nextInt(); System.out.println(" "); int i,j,k,l=1; for(i=1; i<=rows; i++ ){ for(j=1; j<=rows-i; j++){ System.out.print(" "); } for(k=1; k<=i; k++,l++){ System.out.print(l+" "); } System.out.println(" "); } } }
When the above code is executed, it produces the following results:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
Pyramid Pattern 6
Program
import java.util.Scanner; public class Pyramid_pattern6{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); //Scanner class in java System.out.print("Enter the rows you want"); int rows=sc.nextInt();//taking rows from user System.out.println(" "); System.out.print("Here your pattern\n"); int count=1; //initializing count with 1 int i,j;//variable declaration for(i=rows; i>=1; i-- ){//outer for loop(parent) for(j=1; j<=i*2; j++){ System.out.print(" ");//create space from te begining of each row }//inner for loop 1(child) for(j=i; j<=rows; j++){//print right part of Piramid System.out.print(j+" "); }//inner for loop 2 for(j=rows-1; j>=i; j--){ System.out.print(j+" "); //print left part of Piramid }//inner for loop 3 System.out.println(); count++;//count increse with 1 } } }
When the above code is executed, it produces the following results:
Enter the rows of you want Here your pattern 9 8 9 8 7 8 9 8 7 6 7 8 9 8 7 6 5 6 7 8 9 8 7 6 5 4 5 6 7 8 9 8 7 6 5 4 3 4 5 6 7 8 9 8 7 6 5 4 3 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
Suggested for you
Do-while loop in Java language
Similar post
Java program to print star pyramid pattern
C program to print star pyramid pattern
C++ program to print star pyramid pattern
Python program to print star pyramid pattern
Floyd’s triangle number pattern using for loop in C
Floyd’s triangle pattern using nested for loop in Java
Floyd’s triangle pattern using nested while loop in Java