Uncategorized

Diamond pattern program in Java language

Diamond pattern program in Java language

In this article, we will discuss the Diamond star pattern program in Java language

we can create star number, alphabet, binary patterns using loops (for, while and do-while loop) in Java language

In this post, we are going to learn how to create a diamond pattern(Symbol) in Java language using for, while and do-while loop

Diamond pattern program in Java language

Diamond program using for loop in java language

This program allows the user to enter the number of rows and the symbol then the program displays the diamond pattern with the given symbol using for loop in Java language

Program 1

import java.util.Scanner;
class DiamondPatternfor{
public static void main(String args[]){
int i,j;
Scanner scan=new Scanner(System.in);
System.out.print("Enter the number of rows");
int rows=scan.nextInt();
System.out.println("Enter symbol as you wish: "); 
char ch=scan.next().charAt(0);
//read number input from user


for(i=1; i<=rows; i++){    //iterates through row
//outer for loop display half of upper part in diamond
    for(j=1; j<=rows-i; j++){
     System.out.print(" ");   //print space
  }
  for(j=1; j<=i*2-1; j++){
     System.out.print(ch);//print given symbol
  }
  System.out.println();
}
  
for(i=rows-1; i>=0; i--){
//outer for loop display half of lower part in diamond
    for(j=1; j<=rows-i; j++){
     System.out.print(" ");
  }
  for(j=1; j<=i*2-1; j++){
     System.out.print(ch);
  }
  System.out.println();//move to next line
  }
  }
  }

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

 

Diamond program using while loop in Java language

This program allows the user to enter the number of rows and the symbol then the program displays the diamond pattern with the given symbol using while loop in Java language

Program 2

import java.util.Scanner;
class DiamondPatternwhile{
public static void main(String args[]){
int i,j;
Scanner scan=new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows=scan.nextInt();
System.out.println("Enter symbol as you wish: "); 
char ch=scan.next().charAt(0);
//read number input from user

i=1;
while(i<=rows){//first outer whule loop iterates through the row
  j=1;
    while(j<=rows-i){
     System.out.print(" ");//print star
      j++;
  }
  j=1;
  while(j<=i*2-1){
     System.out.print(ch);//print given symbol
      j++;
  }
  System.out.println();//move to next line
   i++;
}
  i=rows-1; 
while(i>=0){//second outer while loop
  j=1;
    while(j<=rows-i){
     System.out.print(" ");//print space - inner wile loop
      j++;
  }
  j=1;
  while(j<=i*2-1){
     System.out.print(ch);//print given symbol - inner while loop
     j++;
  }
  System.out.println();
  i--;
  }
  }
  }

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

 

Diamond program using do-while loop in Java language

This program allows the user to enter the number of rows and the symbol then the program displays the diamond pattern with the given symbol using do-while loop in Java language

Program 3

import java.util.Scanner;
class DiamondPatterndowhile{
public static void main(String args[]){
int i,j;
Scanner scan=new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows=scan.nextInt();
System.out.println("Enter symbol as you wish: "); 
char ch=scan.next().charAt(0);
//read number input from user

i=1;
do{
  j=1;
    do{
     System.out.print(" ");
      j++;
  }while(j<=rows-i);
  j=1;
  do{
     System.out.print(ch);
      j++;
  }while(j<=i*2-1);
  System.out.println();
   i++;
}while(i<rows);
  i=rows-1; 
do{
  j=1;
   do{
     System.out.print(" ");
      j++;
  } while(j<=rows-i);
  j=1;
  do{
     System.out.print(ch);
     j++;
  }while(j<=i*2-1);
  System.out.println();
  i--;
  }while(i>0);
  }
  }

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

 

Suggested for you

for loop in Java language

while loop in Java language

do-while loop in Java language

 

Similar post

pyramid pattern program in C- using loops

Java pyramid pattern program – using loops

C++ pyramid pattern program – using loops

C++ code to Inverted Pyramid pattern

Java code to Inverted Pyramid pattern

C code to Inverted Pyramid pattern

C language Hollow Pyramid pattern

Java language Hollow Pyramid pattern

Display Hollow Inverted Pyramid pattern in C language

Display Hollow Inverted Pyramid pattern in Java language

Display  Hollow diamond shape in C++ language

Display hollow diamond shape in C language

Display Hollow diamond shape in Java language

Display diamond shape in C language

Display diamond shape in C++ language

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

4 weeks ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

4 weeks ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.