Triangle programs in java – Java Program to Print Left Triangle Star Pattern

Program to Print Left Triangle Star Pattern

Triangle programs in java: In this article we are going to see how to print the left triangle star program.

Example-1

When row value=4
         * 
      * *
   * * * 
* * * *
Example-2

When row valu5         
               * 
            * * 
         * * * 
      * * * * 
   * * * * *

Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.

Approach:

  • Enter total row and store it in an int variable ‘row’.
  • Take first for loop for rows count.
  • Take second/inner for loop for printing space.
  • Take another inner for loop for printing * .
  • Then go on printing the star symbol.

JAVA Code:

Method-1 : Static Star Character

Java Program to Print Left Triangle Star Pattern

import java.util.*;
public class Main 
{    
    public static void main(String args[])   
    {   
    // taking variable for loop iteration and row .
    int row,r,c;
    //creating object 
    Scanner s = new Scanner(System.in);
    // entering the number of row
    System.out.print("Enter rows : ");
    row = s.nextInt();
    //iteration for no. of row 
        for (r=0; r<row; r++) 
        {
            //iteration for printing space 
             for (c=2*(row-r); c>=0; c--)         
               System.out.print(" ");  
            //iteration for printing stars
            for (c=0; c<=r; c++ ) 
                System.out.print("* "); 
            // printing new 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;
    //creating object 
    Scanner s = new Scanner(System.in);
    // entering the number of row
    System.out.print("Enter rows : ");
    row = s.nextInt();
    // entering the symbol
    System.out.print("Enter symbol : ");
    char symbol = s.next().charAt(0);
    //iteration for no. of row 
        for (r=0; r<row; r++) 
        {
            //iteration for printing space 
             for (c=2*(row-r); c>=0; c--)         
               System.out.print(" ");  
            //iteration for printing stars
            for (c=0; c<=r; c++ ) 
                System.out.print(symbol+" "); 
            // printing new line .
            System.out.println();   
        }   
    }
}  
  

Output:

Enter rows : 5
Enter Symbol : @
                @
            @@
        @@@
    @@@@
@@@@@

Explanation :

Iteration-1

r=0 (passes the first for loop condition) as it will iterate up to r<row times.

First inner for loop will print the space 2*(row-r) time that means 10 times.

then 2nd inner for loop will print the * one time because it will iterate up to c<=r times.

So, Star will be printed one time.

          *

Iteration-2

r=1 (passes the first for loop condition) as it will iterate up to r<row times.

first inner for loop will print the space 2*(row-r) time that means 8 times.

then 2nd inner for loop will print the * two times because it will iterate up to c<=r times.

Star will be printed 2 times.

        * *

Iteration-3

r=2 (passes the first for loop condition) as it will iterate up to r<row times.

First inner for loop will print the space 2*(row-r) time that means 6 times.

Then 2nd inner for loop will print the * three times because it will iterate up to c<=r times.

Star will be printed 3 times.

      * * *

Iteration-4

r=3 (passes the first for loop condition) as it will iterate up to r<row times.

First inner for loop will print the space 2*(row-r) time that means 4 times.

Then 2nd inner for loop will print the * four times because it will iterate up to c<=r times.

Star will be printed 4 times.

    * * * *

Iteration-5

r=4 (passes the first for loop condition) as it will iterate up to r<row times.

First inner for loop will print the space 2*(row-r) time that means 2 times.

Then 2nd inner for loop will print the * five times because it will iterate up to c<=r times.

Star will be printed 5 times.

  * * * * *

Now when r=6, first for loop condition will fail so no other loops will be executed.

At last, we will see a pattern like this as output.

               * 
            * * 
         * * * 
      * * * * 
   * * * * *

C Code to Print to Print Left Triangle Star Pattern:

#include <stdio.h>
int main()
{
    int row;
    printf("Enter rows : ");
    scanf("%d",&row);
    for(int r=1; r<=row; r++)
    {
        // Printing spaces
        for(int c=r; c<row; c++)
        {
            printf(" ");
        }
        // Printing star
        for(int c=1; c<=r; c++)
        {
            printf("*");
        }
        // Printing new line
        printf("\n");
    }
    return 0;
}
Output:
Enter rows : 5
           *
         **
       ***
     ****
   *****

C++ Code to Print Left Triangle Star Pattern:

#include<iostream>
using namespace std;
int main()
{
    
    int row;
   cout << "Enter rows : ";
   cin >> row;
    int space=10;
    for(int r=0; r<row; r++)
    {
        for(int c=0; c<space; c++)
            cout<<" ";
        space = space-2;
        for(int c=0; c<=r; c++)
            cout<<"* ";
        cout<<endl;
    }
    cout<<endl;
    return 0;
}
Output:


Enter rows : 5
         
    *
   **    
  *** 
 ****
*****

Related Java Star Pattern Programs: