Diamond pattern

Java program to Display diamond number pattern using while loop

Java program to Display diamond number pattern using while loop

In this tutorial, we will discuss the concept of Java program to Display diamond number pattern using while loop

In this post, we will learn about how to print diamond number pattern using while loop in Java language.

 

We can use for loop, while loop or do while loop to display different diamond Number patterns.

Here, we will use while loop to print diamond Number patterns in Java language

 

The following Java program requests the user to enter the number of rows as the user wanted and the program will display the number of rows in diamond number pattern according to the user input

Program 1

Diamond number pattern 1

import java.util.Scanner;
class DiamondNumberWhile1{
public static void main (String args[]){

Scanner scan=new Scanner(System.in);
//create a scanner object for input

System.out.print("Enter the number of rows: ");
int rows=scan.nextInt();//reads rows from user
int row=1;
int i=rows/2;
while( i>0){    //print upper part
int j=1; 
    while(j<=i){
      System.out.print(" ");//print space
       j++;
}
   j=1; 
while(j<=row){
      System.out.print(row+" ");
       j++;
}
System.out.println();//move to next line
row++;
 i--;
}

i=0;
while( i<=rows/2){ //print lower part
    int j=1; 
  while (j<=i){
      System.out.print(" ");//print space
       j++;
}

j=row;
while( j>=1){ 
      System.out.print(row+" ");//print number
       j--;
}
System.out.println();//move to next line
row--;
 i++;
        }
    }
}

When the above code is executed, it produces the following result

Enter the number of rows: 8
        
             1
          2   2
       3   3   3
     4  4   4   4
  5  5   5   5   5
     4  4   4   4  
       3   3   3
          2   2
             1

 

Program 2

Diamond number pattern 2

import java.util.Scanner;
class DiamondNumberWhile2{
public static void main (String args[]){

Scanner scan=new Scanner(System.in);
//create a scanner object for input

System.out.print("Enter the number of rows: ");
int rows=scan.nextInt();//reads rows from user
int i=1; 
while(i<=rows){ 
int j=1;    //print upper part
    while(j<=rows-i){
      System.out.print(" ");//print space
       j++;
}
int k=i; 
while(k>=1){
      System.out.print(k);//print number
       k--;
}
int l=2; 
while(l<=i){
      System.out.print(l);//print number
       l++;
}
System.out.println();
 i++;
}

i=rows-1; 
while(i>=1){  //print lower part
    for(int j=0; j<=rows-1-i; j++){
      System.out.print(" ");//print space
}
int k=i; 
while(k>=1){
      System.out.print(k);
       k--;
}
int l=2; 
while(l<=i){
      System.out.print(l);
       l++;
}
System.out.println();
 i--;
        }
    }
}

When the above code is executed, it produces the following result

Enter the number of rows: 6
          1
        212
      32123
    4321234
  543212345
65432123456
  543212345
    4321234
      32123
        212
          1

 

Program 3

Diamond number pattern 3

import java.util.Scanner;
class DiamondNumberWhile3{
public static void main (String args[]){

Scanner scan=new Scanner(System.in);
//create a scanner object for input

System.out.print("Enter the number of rows: ");
int rows=scan.nextInt();//reads rows from user
for(int i=1; i<=rows; i++){    //print upper part
    for(int j=1; j<=rows-i; j++){
      System.out.print(" ");
}
for(int k=1; k<=i; k++){
      System.out.print(k+" ");//print number with space to upper part
}
System.out.println();
}

for(int i=1; i<=rows-1; i++){  //print lower part
    for(int j=1; j<=i; j++){
      System.out.print(" ");//print space
}
for(int k=1; k<=rows-i; k++){
      System.out.print(k+" ");//print number with space to lower part
} 
System.out.println();//move to next line
        }
    }
}

When the above code is executed, it produces the following result

 

Enter the number of rows: 6
         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
    1  2  3  4
      1  2  3 
        1  2
          1

 

Similar post

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

Hollow pyramid triangle pattern in C++ language

Pyramid star pattern in Cpp

Pyramid star pattern in C

Pyramid star pattern in Java 

Rhombus pattern in Java using for loop

Rhombus pattern in C using while loop

Rhombus pattern in C++ using do-while loop

Display Pyramid star pattern in C

Display Pyramid star pattern  in Java

Parallelogram and Hollow Parallelogram star pattern in Java

Parallelogram and Hollow Parallelogram star pattern in C Language

C++ program to Display diamond number pattern using while loop

C  program to Display diamond number pattern using while loop

 

Suggested post

For loop in C++ language

For loop in Java language

For loop in C language

For loop in Python language

while loop in C++ language

while loop in Java language

while loop in C language

while 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

 

If statements in Java  language

Nested if statements in Java  language

 

 

 

Operator in Java language

Data type in Java language

while loop in Java language

Nested while loop in Java language

 

C program to Display diamond number pattern using while loop
Java program to Integrated triangle patterns using for loop
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…

1 month 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…

1 month ago