Java program to print Rhombus pattern using star or any character :
In this tutorial, we will show you how to print a Rhombus in Java. We can print the Rhombus using any character like *,&,$ etc. In this example, we are using ‘for’ loop.But instead of ‘for’, we can also use ‘while’ or ‘do-while’ loop.
The output will look as like below :
******
******
******
******
******
******
Using one loop, we will print each line.
- For the first line , no spaces are added before the stars.
- For the 2nd line, one space,for the 3rd line, two spaces etc.
- Print the spaces first and then print the stars. After printing the stars, print a new line. That’s it.
We are using the ‘Scanner’ class to take the input from the users : The size of the Rhombus and the character used to print it.
Program :
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int size;
Character c;
System.out.println("Enter size of the Rhombus : ");
size = sc.nextInt();
System.out.println("Which character you want to use : ");
c = sc.next().charAt(0);
for (int row = 0; row < size; row++) {
//first print the space
for (int space = size - row; space < size; space++) {
System.out.print(" ");
}
//print the character
for (int i = 0; i < size; i++) {
System.out.print(c);
}
//add a newline
System.out.println();
}
}
}
Example 1 :
Enter size of the Rhombus :
6
Which character you want to use :
*
******
******
******
******
******
******
Example 2 :
Enter size of the Rhombus :
8
Which character you want to use :
&
&&&&&&&&
&&&&&&&&
&&&&&&&&
&&&&&&&&
&&&&&&&&
&&&&&&&&
&&&&&&&&
&&&&&&&&
Similar tutorials :
- Java program to print triangle and reverse triangle
- Java program to print all files and folders in a directory in sorted order
- Java Program to print the sum of square series 1^2 +2^2 + ….+n^2
- Java program to print the ASCII value of an integer
- Java program to print a rectangle using any special character
- Java program to print a square using any character