pyramid triangle

pyramid number pattern in Java using for loop

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

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

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

 

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

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

Pyramid number 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

Floyd’s number pattern

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

For loop in Java language

For loop in C++ language

For loop in C language

For loop in Python language

 

While loop in Java language

While loop in C language

While loop in C++ language

While loop in Python language

 

Do-while loop in Java language

Do-while loop in C language

Do-while loop in C++ 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

 

 

Java program to display star Pyramid pattern
Hollow Pyramid Pattern C Programming
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago