Program to Print Rhombus Star Pattern
In this article we are going to see how to print the rhombus star pattern.
Example-1 When row value=4 **** **** **** ****
Example-2 When row value=5 ***** ***** ***** ***** *****
Now, let’s see the actual program to print it.
If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.
Approach:
- Enter total row and store it in an integer variable
rows
. - Take one outer for loop to iterate the rows.
- Take two inner for loops, one to print the space and the other one to print the star.
- After each iteration print a new line.
JAVA Code:
Method-1 : Static Star Character
import java.util.Scanner; class pattern { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Rows : "); //Taking input from user int rows=scan.nextInt(); //Outer loop for (int r=1; r<=rows; r++) {//Inner loop 1 for (int c=rows-1; c>=r; c--) { System.out.print(" "); } // Prints star in decreasing order for (int k=1; k<=rows; k++) {//Inner loop 2 System.out.print("*"); } //Prints a newline System.out.println(); } } }
Output: Rows : 5 ***** ***** ***** ***** *****
Method-2 : User Input Character
import java.util.Scanner; class pattern { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Rows : "); //Taking input from user int rows=scan.nextInt(); System.out.print("Symbol : "); //Taking random character input from user char s=scan.next().charAt(0); //Outer loop for (int r=1; r<=rows; r++) {//Inner loop 1 for (int c=rows-1; c>=r; c--) { System.out.print(" "); } // Prints star in decreasing order for (int k=1; k<=rows; k++) {//Inner loop 2 System.out.print(s); } //Prints a newline System.out.println(); } } }
Output: Rows : 5 Symbol : ^ ^^^^^ ^^^^^ ^^^^^ ^^^^^ ^^^^^
Explanation :
Let’s understand the program by going through the detailed explanation.
We have taken rows value as 5.
Iteration-1
r=1, goes into the first inner loop and prints 4 space as it will iterate upto c>=r
The second for loop prints five star(‘*’) for k<=rows
(i.e. 5).
*****
Iteration-2
r=2, goes into the first inner loop and prints 3 space as it will iterate upto c>=r.
The second for loop prints five star(‘*’) for k<=rows
(i.e. 5).
*****
Iteration-3
r=3, goes into the first inner loop and prints 2 space as it will iterate upto c>=r
The second for loop prints five star(‘*’) for k<=rows
(i.e. 5).
*****
Iteration-4
r=4, goes into the first inner loop and prints 1 space as it will iterate upto c>=r
The second for loop prints five star(‘*’) for k<=rows
(i.e. 5).
*****
Iteration-5
r=5, does not go into first inner loop as conditions fails c>r
(while current c value is row-1 i.e 4 but condition is c>r means 4>5 ios false). So no space will be printed.
The second for loop prints five star(‘*’) for k<=rows
(i.e. 5).
*****
After this r >rows i.e. 6 so program will come out of the loop.
Now, after end of all iteration we will see the complete pattern is printed on the output screen like this.
***** ***** ***** ***** *****
C Code:
#include <stdio.h> int main(int argc, char const *argv[]) { printf("Enter rows : "); int rows; //Taking row as input from user scanf("%d", &rows); //Outer loop for (int r = 1; r <= rows; r++) { //Inner loop for (int c = rows - 1; c >= r; c--) { printf(" "); } // Print stars in decreasing order for (int k = 1; k <= rows; k++) { //Inner loop 2 printf("*"); } //To print a newline printf("\n"); } return 0; }
Output: Enter rows : 5 ***** ***** ***** ***** *****
C++ Code:
#include <iostream> using namespace std; int main(int argc, char const *argv[]) { cout << "Enter rows : "; int rows; //Taking row as input from user cin >> rows; //Outer loop for (int r = 1; r <= rows; r++) { //Inner loop 1 for (int c = rows - 1; c >= r; c--) { cout << " "; } // Print stars in decreasing order for (int k = 1; k <= rows; k++) { //Inner loop 2 cout << "*"; } //To print a newline cout << endl; } return 0; }
Output: Enter rows : 5 ***** ***** ***** ***** *****
Related Java Star Pattern Programs: