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.

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 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.

Test Yourself:

  1. Print staircase in java?
  2. Print staircase pattern?
  3. Staircase hackerrank solution in java?
  4. Right staircase pattern in java?
  5. Pattern program in java?
  6. Staircase pattern code?
  7. Triangle number pattern program in java?
  8. Print staircase in c?
  9. Java program to print star pattern?
  10. Java program to print stars?
  11. C program to print staircase pattern?
  12. Java program to print star pattern using while loop?
  13. Java program to print a star pattern?
  14. Java program to print pattern of numbers and stars?
  15. Java program to print 12345 pattern?
  16. Java program to print number pattern?

Related Java Number Pattern Programs: