Program to Print Hollow Right Angle Tringle Star Pattern
In this article we are going to see how to print Hollow right angle Tringle star program.
Example-1 When row value=4 * ** * * ****
Example-2: When row value=5 * ** * * * * *****
Now, let’s see the actual program for printing it.
Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.
Approach:
- Enter total row and store it in an integer variable
row
. - Take first for loop to print all rows.
- Take first inner for loop to print column values i.e., first inner for loop will print all the spaces in the column. With this print stars and spaces according to condition , i.e.
if(c==1 ||c==r || r==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++) { //inner for loop for(c=1; c<=r; c++) if(c==1 ||c==r || r==row ) System.out.print("*"); else System.out.print(" "); System.out.print("\n");//move to next line } } }
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,d; char ran; //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 : "); ran = s.next().charAt(0); //outer for loop for(r=1; r<=row ; r++) { //inner for loop for(c=1; c<=r; c++) if(c==1 ||c==r || r==row ) System.out.print(ran); else System.out.print(" "); System.out.print("\n");//move to next line } } }
Output : Enter rows : 5 Enter character : @ @ @@ @ @ @ @ @@@@@
C Code:
#include <stdio.h> int main() { int r, row, c ,d; printf("Enter rows: "); scanf("%d", &row); for(r=1; r<=row ; r++) { //inner for loop for(c=1; c<=r; c++) if(c==1 ||c==r || r==row ) printf("*"); else printf(" "); printf("\n");//move to next linne } 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=1; r<=row ; r++) { //inner for loop for(c=1; c<=r; c++) if(c==1 ||c==r || r==row ) cout << "*" ; else cout << " "; cout << "\n" ;//move to next line } return 0; }
Output : Enter rows : 5 * ** * * * * *****
Related Java Star Pattern Programs: