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 C++ language

For loop in Java language

For loop in C language

For loop in Python language

Nested for loop in Java language

Nested for loop in C++ language

Nested for loop in C language

Nested for loop in Python language

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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago