Program to Print Hollow Left Angle Tringle Star Pattern
In this article we are going to see how to print Hollow left angle Tringle star program.
Example-1
When row value=7
*
**
* *
* *
* *
* *
*******
Example-2: When row value=5 * ** * * * * *****
Now, let’s see the actual program printing it.
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
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. - Check the condition
if(r==1 || r==row)if condition satisfies second inner for loop will execute and print all the*. - If condition fails then third inner for loop will execute . for each iteration check the condition
if(c==1 || c==r)if condition satisfies then*will be printed elsespacewill be printed . - 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++)
{
for(c=1;c<=row-r;c++)
System.out.print(" ");
if(r==1 || r==row)
for(c=1;c<=r;c++)
System.out.print("*");
else
{
for(c=1;c<=r;c++)
{
if(c==1 || c==r)
System.out.print("*");
else
System.out.print(" ");
}
}
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;
char ran_char;
//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_char = s.next().charAt(0);
//outer for loop
for(r=1;r<=row;r++)
{
for(c=1;c<=row-r;c++)
System.out.print(" ");
if(r==1 || r==row)
for(c=1;c<=r;c++)
System.out.print(ran_char);
else
{
for(c=1;c<=r;c++)
{
if(c==1 || c==r)
System.out.print(ran_char);
else
System.out.print(" ");
}
}
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++)
{
for(c=1;c<=row-r;c++)
printf(" ");
if(r==1 || r==row)
for(c=1;c<=r;c++)
printf("*");
else
{
for(c=1;c<=r;c++)
{
if(c==1 || c==r)
printf("*");
else
printf(" ");
}
}
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++)
{
for(c=1;c<=row-r ; c++)
cout <<" ";
if(r==1 || r==row)
for(c=1;c<=r;c++)
cout <<"*";
else
{
for(c=1;c<=r;c++)
{
if(c==1 || c==r)
cout <<"*";
else
cout <<" ";
}
}
cout <<"\n";
}
return 0;
}
Output :
Enter rows : 5
*
**
* *
* *
*****
Related Java Star Pattern Programs: