Java Program to Print Ladder Star Pattern

Program to Print Ladder Star Pattern

In this article we are going to see how to print ladder star program.

Example-1

When row value=2

*    *
*    *
*****
*    *
*    *
*****
*    *
*    *
Example-2:

When row value=3
*    *
*    *
*****
*    *
*    *
*****
*    *
*    *
*****
*    *
*    *

Now, let’s see the actual program printing  it.

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Approach:

  • Enter total row and store it in an integer variable row.
  • Take first for loop to print the row value and a star for each row.
  • For each iteration print 2 side stars.
  • And for each condition (r<row) print the stair bar.
  • Then go on printing the star symbol according to loop.

JAVA Code:

import java.util.*;
public class Main 
{    
    public static void main(String args[])   
    {   
    // taking variable for loop iteration and row .
    int row,r;
    //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 = 0; r <= row ; r++)
    {
          // Printing the sub-pattern 1 row+1 times
        System.out.println("*   *");
        System.out.println("*   *");
        // Printing the sub-pattern 2 row times
        if (r < row)
            System.out.println("*****" );
    }
  }
}
Output :

Enter row :  3

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

C Code:

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

Enter row :  3

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

C++ Code:

#include <iostream>
using namespace std;
int main()
{
   int row, r , c ,d ;
   cout << "Enter  rows: ";
   cin >> row;
   for ( r = 0; r <= row ; r++)
    {
        cout << "*   *" ;
        cout << "\n" ;
        cout << "*   *" ;
        cout << "\n" ;
        if (r < row)
            cout << "*****" ;
            cout << "\n" ;
    }
   return 0;
}
Output :

Enter row :  3

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

Related Java Star Pattern Programs: