Program to Print Hollow Inverted Mirrored Right Angle Triangle Star Pattern
In this article we are going to see how to print Hollow inverted mirrored right angle tringle star program.
Example-1 When row value=4 **** * * ** *
Example-2: When row value=5 ***** * * * * ** *
Now, let’s see the actual program printing it.
Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output
Approach:
- Enter total row and store it in an integer variable
row
. - Take first for loop to print upper hollow part.
- Take first inner for loop to print spaces in increasing order.
- Take second inner for loop to print stars according to the condition
if(r == 1 || c == r|| c == row)
- Then go on printing the star symbol according to loop.
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,d; //creating object Scanner s = new Scanner(System.in); // entering the number of row System.out.print("Enter rows : "); row = s.nextInt(); //outer for loop for (r=1; r<=row; r++) { // Print space in increasing order for (c=1; c<r; c++) System.out.print(" "); // Print star in decreasing order for (c=row; c>=r; c--) { if( r == 1 || c == r || c == row) System.out.print("*"); else System.out.print(" "); } System.out.println(); } } }
Output : Enter Rows : ***** * * * * ** *
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,d; char ch; //creating object Scanner s = new Scanner(System.in); // entering the number of row System.out.print("Enter rows : "); row = s.nextInt(); // entering any random character System.out.print("Enter character : "); ch = s.next().charAt(0); //outer for loop for (r=1; r<=row; r++) { // Print space in increasing order for (c=1; c<r; c++) System.out.print(" "); // Print star in decreasing order for (c=row; c>=r; c--) { if( r == 1 || c == r || c == row) System.out.print(ch); else System.out.print(" "); } System.out.println(); } } }
Output : Enter Rows : 5 Enter Character : a aaaaa a a a a aa a
C Code:
#include <stdio.h> int main() { int r, row, c ,d; printf("Enter rows: "); scanf("%d", &row); for (r=1; r<=row; r++) { // Print space in increasing order for (c=1; c<r; c++) printf(" "); // Print star in decreasing order for (c=row; c>=r; c--) { if( r == 1 || c == r || c == row) printf("*"); else printf(" "); } printf("\n"); } return 0; }
Output : Enter Rows : ***** * * * * ** *
C++ Code:
#include <iostream> using namespace std; int main() { int row, r , c ,d ; cout << "Enter rows: "; cin >> row; for (r=1; r<=row; r++) { // Print space in increasing order for (c=1; c<r; c++) cout << " "; // Print star in decreasing order for (c=row; c>=r; c--) { if( r == 1 || c == r || c == row) cout << "*"; else cout << " "; } cout << "\n"; } return 0; }
Output : Enter Rows : ***** * * * * ** *
Related Java Star Pattern Programs: