Java Program to Print Double Sided Stair Case Character Pattern

Program to Print Double Sided Stair Case Character Pattern

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

In this article we are going to see how to print double sided staircase character pattern.

Example-1

When row value=8

           A B 
           A B
        A B C D
        A B C D
     A B C D E F
     A B C D E F 
  A B C D E F G H
  A B C D E F G H
Example-2:

When row value=10

            A B        
            A B
         A B C D     
         A B C D
      A B C D E F    
      A B C D E F 
  A B C D E F G H   
  A B C D E F G H
A B C D E F G H I J
A B C D E F G H I J

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

Java Code to Print Double Sided Stair Case Character 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;
        // Starting ASCII value taken 65
        int asciiAlpha = 65;
        //creating object of Scanner class
        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 spaces
            for (c = 0; c < k; c++)
                System.out.print(" "+(char)(c + asciiAlpha));
            System.out.println();
        }
    }
}
Output :

Enter rows : 8        

         A B       
         A B      
      A B C D     
      A B C D    
    A B C D E F    
    A B C D E F 
A B C D E F G H   
A B C D E F G H

C Code to Print Double Sided Stair Case Character Pattern

#include <stdio.h>
int main() 
{
    int row,r,c,k ;
    int asciiAlpha = 65;
    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("%c ",(char)(c + asciiAlpha));
             printf("\n");
        }
   return 0;
}
Output :

Enter rows : 8        

         A B       
         A B      
      A B C D     
      A B C D    
   A B C D E F    
   A B C D E F 
A B C D E F G H   
A B C D E F G H

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 Character Pattern Programs: