Diamond pattern

Diamond number pattern in Java language

Diamond number pattern in Java language

In this tutorial, we will discuss a simple concept of the diamond number  pattern  in Java language

In this post, we will learn how to create diamond number patterns using loops.

We can use for loop, while loop or do while loop to display different diamond patterns in Java programming language.

 

Diamond pattern in Java language

Here, we will use for loop to print different diamond  patterns

Program 1

Diamond  pattern 1

import java.util.Scanner;
class DoubleNumber2{
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(" ");//print space
}
for(int k=i; k>=1; k--){
      System.out.print(k);
}
for(int l=2; l<=i; l++){
      System.out.print(l);
}
System.out.println();//move to next line
}

for(int i=rows-1; i>=1; i--){  //print lower part
    for(int j=0; j<=rows-1-i; j++){
      System.out.print(" ");//print space
}
for(int k=i; k>=1; k--){
      System.out.print(k);
}
for(int l=2; l<=i; l++){
      System.out.print(l);
}
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
        212
      32123
    4321234
  543212345
65432123456
  543212345
    4321234
      32123
        212
          1

 

 

Program 2

Diamond pattern 2

import java.util.Scanner;
class DoublePyramid4{
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(" ");//print space
}
for(int k=1; k<=i; k++){
      System.out.print(k+" ");
}
System.out.println();//move to next line
}

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+" ");
}
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

Program 3

Diamond  pattern 3

import java.util.Scanner;
class DiamondNumber3{
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;

for(int i=rows/2; i>0; i--){    //print upper part
    for(int j=1; j<=i; j++){
      System.out.print(" ");//print space 
}
for(int j=1; j<=row; j++){
      System.out.print(row+" ");
}
System.out.println();//move to next line
row++;
}

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

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

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

 

Similar post

Diamond  patterns in C language

Diamond patterns in C++ language

 integrated pyramid star pattern in C

integrated pyramid star pattern in Java

C++ code to Double pyramid star pattern

Java  code to Double pyramid star pattern

Java program to Display diamond number pattern using while loop

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

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

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

C  program to Display diamond number pattern using while loop

 

Suggested for you

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

Nested while loop in Java language

Operator in C language

Data type in C  language

variable in C  language

Nested while loop in C language

input-output function in C

Class and main method in Java language

Operator in C++ language

Data type in C++  language

variable in C++  language

 

 

 

 

C code to display Diamond number pattern
Generate Double pyramid number pattern in Java
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

Explanation of one dimensional array (Data structure)

Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…

2 days ago

Python program to calculate the sum of odd and even numbers in a list

Python program to calculate the sum of odd and even numbers in a list In…

5 days ago

Python code to Calculate sum of odd and even in a list

Python code to Calculate sum of odd and even in a list In this tutorial,…

1 week ago

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 weeks ago

C# inverted full pyramid star pattern

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

1 month ago

C# Full Pyramid star pattern program

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

2 months ago