Java Program to Print Stair Case Star Pattern

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

Example-1

When row value=8

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

When row value=10

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

Now, let’s see the actual program to print it.

Approach:

  • Enter total row and store it in an integer variable row.
  • Take first outer for loop to keep track of number of rows.
  • Take first inner for loop for printing stars.
  • 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 ,c,r,k;
        //creating object 
        Scanner s = new Scanner(System.in);
        // entering the number of row
        System.out.print("Enter rows : ");
        row = s.nextInt();
       
  
 for (r = 1; r <= row ; r++)
          {
            if(r % 2 != 0)
                k = r + 1 ;
            else
                k = r ;
            for (c = 0; c < k; c++)
                System.out.print("* ");
            System.out.println();
        }
    }
}
Output :

Enter row :  10

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

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 ,c,r,k;
        //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 st = s.next().charAt(0);
       
  
 for (r = 1; r <= row ; r++)
          {
            if(r % 2 != 0)
                k = r + 1 ;
            else
                k = r ;
            for (c = 0; c < k; c++)
                System.out.print(st+" ");
            System.out.println();
        }
    }
}
Output :

Enter row :  10
Enter character : *

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

C Code:

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

Enter row :  10

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

C++ Code:

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

Output :

Enter row :  10

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

Leave a Comment