Java Program to Print Stair Case Number Pattern

Print Stair Case Number Pattern

In the previous article, we have discussed Java Program to Print Greater Than Symbol Number Pattern

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

Example-1

When number of rows : 5

0 1 
0 1 
0 1 2 3 
0 1 2 3 
0 1 2 3 4 5 

Example-2

When number of rows : 6

0 1 
0 1 
0 1 2 3 
0 1 2 3 
0 1 2 3 4 5 
0 1 2 3 4 5

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 numbers.
  • Then go on printing the numbers according to loop.

Java Code to Print Stair Case Number Pattern

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(c+" ");
            System.out.println();
        }
    }
}
Output:

Enter rows : 6

0 1 
0 1 
0 1 2 3 
0 1 2 3 
0 1 2 3 4 5 
0 1 2 3 4 5

C Code to Print Stair Case Number Pattern

#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("%d ",c);
             printf("\n");
        }
   return 0;
}
Output:

Enter rows : 6

0 1 
0 1 
0 1 2 3 
0 1 2 3 
0 1 2 3 4 5 
0 1 2 3 4 5

C++ Code to Print Stair Case Number Pattern

#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 << c << " ";
            cout << "\n";
        }
   return 0;
}
Output:

Enter rows : 6

0 1 
0 1 
0 1 2 3 
0 1 2 3 
0 1 2 3 4 5 
0 1 2 3 4 5

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Number Pattern Programs: