In this article we are going to see how to print the inverted mirrored right triangle star program.
Example-1 When row value=4 * * * * * * * * * * * * * * * *
Example-2: When row value=5 * * * * * * * * * * * * * * * * * * * * * * * * *
Now, let’s see how program printing it.
Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.
Approach:
- Enter total row and store it in an integer variable
row
. - Take first outer for loop to print all rows.
- Take first inner for loop to print column values ( it will print space) .
- Take second inner for loop to print column values ( it will print *) .
- Take second outer for loop to print all rows.
- Take first inner for loop to print column values ( it will print space) .
- Take second inner for loop to print column values ( it will print *) .
- Continue printing the star symbols according to the iteration.
JAVA Code:
Method-1 : Static Star Character
import java.util.*; public class Main { public static void main(String args[]) { // taking variable for loop iteration and row . int row,r,c; //creating object Scanner s = new Scanner(System.in); // entering the number of row System.out.print("Enter rows : "); row = s.nextInt(); //iteration for no. of row for(r=0;r<=row;r++) { for(c=0;c<row-r;c++) System.out.print(" "); for(c=0;c< r;c++) System.out.print("* "); System.out.println(); } for(r=row;r>0;r--) { // printing space for( c=row-r;c>=0;c--) System.out.print(" "); // printing stars for( c=1;c<r;c++) System.out.print("* "); //taking to new line System.out.println(); } } }
Output: Enter Rows: 5 * * * * * * * * * * * * * * * * * * * * * * * * *
Method-2 : User Input Character
import java.util.*; public class Main { public static void main(String args[]) { // taking variable for loop iteration and row . int row,r,c; //creating object Scanner s = new Scanner(System.in); // entering the number of row System.out.print("Enter rows : "); row = s.nextInt(); // entering the character System.out.print("Enter any character : "); char sym = s.next().charAt(0); //iteration for no. of row for(r=0;r<=row;r++) { for(c=0;c<row-r;c++) System.out.print(" "); for(c=0;c< r;c++) System.out.print(sym+" "); System.out.println(); } for(r=row;r>0;r--) { // printing space for( c=row-r;c>=0;c--) System.out.print(" "); // printing stars for( c=1;c<r;c++) System.out.print(sym+" "); //taking to new line System.out.println(); } } }
Output: Enter rows : 5 Enter any character : # # # # # # # # # # # # # # # # # # # # # # # # # #
Explanation :
FOR FIRST OUTER FOR LOOP :
Iteration-1
r=0 (passes the first for loop condition) as it will iterate up to r<=row
times.
First inner for loop will print the space row-r
time that means 5 times.
Then 2nd inner for loop will print the * Zero time because it will iterate up to c<r
times.
So, Star will be printed 0 time.
Iteration-2
r=1 (passes the first for loop condition) as it will iterate up to r<=row
times.
first inner for loop will print the space row-r
time that means 4 times.
then 2nd inner for loop will print the * one times because it will iterate up to c< r
times.
Star will be printed 1 times.
*
Iteration-3
r=2 (passes the first for loop condition) as it will iterate up to r<=row
times.
First inner for loop will print the space row-r time that means 3 times.
Then 2nd inner for loop will print the * two times because it will iterate up to c< r
times.
Star will be printed 2 times.
* *
Iteration-4
r=3 (passes the first for loop condition) as it will iterate up to r<=row
times.
First inner for loop will print the space row-r
time that means 2 times.
Then 2nd inner for loop will print the * three times because it will iterate up to c<=r
times.
Star will be printed 3 times.
* * *
Iteration-5
r=4 (passes the first for loop condition) as it will iterate up to r<=row
times.
First inner for loop will print the space row-r
time that means 1 time.
Then 2nd inner for loop will print the * four times because it will iterate up to c<=r
times.
Star will be printed 4 times.
* * * *
Iteration-6
r=5 (passes the first for loop condition) as it will iterate up to r<=row
times.
First inner for loop will print the space row-r
time that means 0 times.
Then 2nd inner for loop will print the * five times because it will iterate up to c<=r
times.
Star will be printed 5 times.
* * * * *
Now when r=6, first for loop condition will fail so no other inner loops will be executed.
FOR SECOND OUTER FOR LOOP :
Iteration-1
r=5(passes the first for loop condition) because it will execute until r>0
Now c=0 (because c= row-r) so first inner for loop will execute 1 time space will be printed because it will execute until c >=0
Now so second inner for loop will execute and print “* ” 4 times because it will iterate up to c<r
time .
* * * *
Iteration-2
r=4(passes the first for loop condition) because it will execute until r>0
Now c=1 (because c= row-r) so first inner for loop will execute 2 time space will be printed because it will execute until c >=0
Now so second inner for loop will execute and print “*” 3 time because it will iterate up to c<r
time .
* * *
Iteration-3
r=3(passes the first for loop condition) because it will execute until r>0
Now c=2 (because c= row-r) so first inner for loop will execute 3 space will be printed time because it will execute until c >=0
Now so second inner for loop will execute and print “*” 2 time because it will iterate up to c<r
time
* *
Iteration-4
r=2(passes the first for loop condition) because it will execute until r>0
Now c=3 (because c= row-r) so first inner for loop will execute 4 time space will be printed because it will execute until c >=0
Now so second inner for loop will execute and print “*” 1 time because it will iterate up to c<r
time
*
Iteration-5
r=1(passes the first for loop condition) because it will execute until r>0
Now c=4 (because c= row-r) so first inner for loop will execute 5 time space will be printed because it will execute until c >=0
Now so second inner for loop will execute and print “*” 0 time because it will iterate up to c<r
time.
Now r =0, so second outer for loop condition will fail. So next for loop will not be executed any more.
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 r, row, c ,d; printf("Enter rows: "); scanf("%d", &row); for(r=0;r<=row;r++) { for(c=0;c<row-r;c++) printf(" "); for(c=0;c< r;c++) printf("* "); printf("\n"); } for(r=row;r>0;r--) { // printing space for( c=row-r;c>=0;c--) printf(" "); // printing stars for( c=1;c<r;c++) printf("* "); //taking to new line printf("\n"); } return 0; }
Output: Enter Rows: 5 * * * * * * * * * * * * * * * * * * * * * * * * *
C++ Code:
#include <iostream> using namespace std; int main() { int row, r , c ,d ; cout << "Enter rows: "; cin >> row; for(r=0;r<=row;r++) { for(c=0;c<row-r;c++) cout << " "; for(c=0;c< r;c++) cout << "* "; cout << "\n"; } for(r=row;r>0;r--) { // printing space for( c=row-r;c>=0;c--) cout << " "; // printing stars for( c=1;c<r;c++) cout << "* "; //taking to new line cout << "\n"; } return 0; }
Output: Enter Rows: 5 * * * * * * * * * * * * * * * * * * * * * * * * *
Related Java Star Pattern Programs: