Program to Print Downward Hollow Tringle Star Pattern
In this article we are going to see how to print the downward hollow triangle star program.
Example-1 When row value=4 ******* * * * * *
Example-2: When row value=5 ********* * * * * * * *
Now, let’s see the actual program to print it.
Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.
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.
- Take second inner for loop to print column values i.e., shaping the tringle with spaces
- Take third inner for loop to print stars according to condition , i.e. if(r==row || (c==1 || c==2*r-1)) .
- 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(); //for loop for rows for( r=row ; r>=1; r--) { //Print each row for( c=row ; c> r; c--) //Printing space for tringle shape System.out.print(" "); for( c=1; c<2*r; c++) { //printing stars if(r==row || (c==1 || c==2*r-1)) System.out.print("*"); else System.out.print(" "); } //moving to next line System.out.println(""); } } }
Output : Enter Row : 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; //creating object Scanner s = new Scanner(System.in); // entering the number of row System.out.print("Enter rows : "); row = s.nextInt(); // entering random character System.out.print("Enter rows : "); char ch = s.next().charAt(0); //for loop for rows for( r=row ; r>=1; r--) {//Print each row for( c=row ; c> r; c--) //Printing space for tringle shape System.out.print(" "); for( c=1; c<2*r; c++) { //printing stars if(r==row || (c==1 || c==2*r-1)) System.out.print(ch); else System.out.print(" "); } //moving to next line System.out.println(""); } } }
Output : Enter Row : 5 Enter character : @ @@@@@@@@@ @ @ @ @ @ @ @
C Code:
#include <stdio.h> int main() { int r, row, c ,d; printf("Enter rows: "); scanf("%d", &row); for( r=row; r>=1; r--) {//Print each row for( c=row; c> r; c--) //Printing space for tringle shape printf(" "); for( c=1; c<2*r; c++) { //printing stars if(r==row || (c==1 || c==2*r-1)) printf("*"); else printf(" "); } //moving to next line printf("\n"); } return 0; }
Output : Enter Row : 5 ********* * * * * * * *
C++ Code:
#include <iostream> using namespace std; int main() { int row, r , c ,d ; cout << "Enter rows: "; cin >> row; for( r=row; r>=1; r--) {//Print each row for( c=row; c> r; c--) //Printing space for tringle shape cout << " "; for( c=1; c<2*r; c++) { //printing stars if(r==row || (c==1 || c==2*r-1)) cout <<"*"; else cout << " "; } //moving to next line cout << "\n"; } return 0; }
Output : Enter Row : 5 ********* * * * * * * *
Related Java Star Pattern Programs: