Java Program to Print Stair Case Character Pattern

Program to Print Stair Case Character Pattern

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

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

Example-1

When row value=8

AB
AB 
ABCD
ABCD
ABCDEF
ABCDEF
ABCDEFGH 
ABCDEFGH
Example-2:

When row value=10

AB
AB 
ABCD 
ABCD 
ABCDEF 
ABCDEF 
ABCDEFGH 
ABCDEFGH
ABCDEFGHIJ 
ABCDEFGHIJ

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

Java Code to Print 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;
        //creating object of scanner class
        Scanner s = new Scanner(System.in);
        // entering the number of row
        System.out.print("Enter rows : ");
        row = s.nextInt();
       
       // ASCII value taken 65
       int asciiAlpha = 65;
  
        for (r = 1; r <= row ; r++)
          {
            if(r % 2 != 0)
                k = r + 1 ;
            else
                k = r ;
            for (c = 0; c < k; c++)
                // printing the character
                System.out.print((char)(c + asciiAlpha));
            // moving to the next line
            System.out.println();
        }
    }
}
Output : 

Enter rows : 10 

AB 
AB 
ABCD 
ABCD 
ABCDEF 
ABCDEF 
ABCDEFGH 
ABCDEFGH 
ABCDEFGHIJ 
ABCDEFGHIJ

C Code to Print 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 = 0; c < k; c++)
                 printf("%c",(c + asciiAlpha));
             printf("\n");
        }
   return 0;
}
Output : 

Enter rows : 10 

AB 
AB 
ABCD 
ABCD 
ABCDEF 
ABCDEF 
ABCDEFGH 
ABCDEFGH 
ABCDEFGHIJ 
ABCDEFGHIJ

C++ Code to Print Stair Case Character Pattern

#include <iostream>
using namespace std;
int main()
{
    int row,r,c,k ;
    // Starting ASCII value taken 65
    int asciiAlpha = 65;
    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 << (char)(c + asciiAlpha);
            cout << "\n";
        }
   return 0;
}
Output : 

Enter rows : 10 

AB 
AB 
ABCD 
ABCD 
ABCDEF 
ABCDEF 
ABCDEFGH 
ABCDEFGH 
ABCDEFGHIJ 
ABCDEFGHIJ

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: