Java Program to Print Hollow Tringle Star Pattern
In this article we are going to see how to print the hollow triangle star program.
Example-1 When row value=4 * * * * * *******
Example-2: When row value=5 * * * * * * * *********
Now, let’s see the actual program printing it.
Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.
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=1; r<=row; r++)
{//Print each row
for( c=r; c<row; 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 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;
//creating object
Scanner s = new Scanner(System.in);
// entering the number of row
System.out.print("Enter rows : ");
row = s.nextInt();
// Entering any Character
System.out.print("Enter character : ");
char ch= s.next().charAt(0);
//for loop for rows
for( r=1; r<=row; r++)
{//Print each row
for( c=r; c<row; 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 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++)
{//Print each row
for( c=r; c<row; 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 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++)
{//Print each row
for( c=r; c<row; 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 rows : 5 * * * * * * * *********
Related Java Star Pattern Programs: