Java Program to Print Double Sided Stair Case Number Pattern

Program to Print Double Sided Stair Case Number Pattern

In the previous article, we have discussed Java Program to Print Stair Case Number Pattern

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

Example-1 

When row value=8 

         0 1 
         0 1 
      0 1 2 3 
      0 1 2 3 
   0 1 2 3 4 5 
   0 1 2 3 4 5 
0 1 2 3 4 5 6 7 
0 1 2 3 4 5 6 7
Example-2:

When row value= 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.

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

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 to print spaces .
  • Take second inner for loop for printing numbers.
  • Then go on printing the number according to loop.

Java Code to Print Double Sided 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;
            //  loop for printing spaces
            for (c = row; c > k; c--)
                System.out.print(" ");
            //  loop for printing numbers
            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 Double Sided 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 = row; c > k; c--)
                 printf(" ");
            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 Double Sided 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 = row; c > k; c--)
                 cout <<" ";
            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: