Pattern – 16: Alpha-Ramp Pattern

Problem Statement: Given an integer N, print the following pattern : 

Here, N = 5.

Examples:

Input Format: N = 3
Result: 
A
B B
C C C

Input Format: N = 6
Result:   
A 
B B
C C C
D D D D
E E E E E
F F F F F F

Solution

Disclaimer: Don’t jump directly to the solution, try it out yourself first.

Problem Link

Approach

There are 4 general rules for solving a pattern-based question : 

  • We always use nested loops for printing the patterns. For the outer loop, we count the number of lines/rows and loop for them.
  • Next, for the inner loop, we focus on the number of columns and somehow connect them to the rows by forming a logic such that for each row we get the required number of columns to be printed.
  • We print the numbers inside the inner loop.
  • Observe symmetry in the pattern or check if a pattern is a combination of two or more similar patterns or not.

In this pattern problem, as we can observe we have to print a right-angled pyramid just like our last pattern but with a twist. Here, in every row, we have to print the same character i times where i is the row number. For example, the 1st row will print 1 A, the 2nd row will print 2 B’s, and so on. So, similar to the previous patterns the outer loop will loop for N times and the inner loop for i times with the character value incrementing each time we enter a new row.

Code

C++ Code

#include <bits/stdc++.h>
using namespace std;

void pattern16(int N)
{
    
      // Outer loop for the number of rows.
      for(int i=0;i<N;i++){
          
          // Defining character for each row.
          char ch = 'A'+i;
          for(int j=0;j<=i;j++){
              
              // same char is to be printed i times in that row.
              cout<<ch<<" ";
              
          }
          // As soon as the letters for each iteration are printed, we move to the
          // next row and give a line break otherwise all letters
          // would get printed in 1 line.
          cout<<endl;
          
      }
}

int main()
{   
    // Here, we have taken the value of N as 5.
    // We can also take input from the user.
    int N = 5;
    pattern16(N);

    return 0;
}

Output 


B B
C C C
D D D D
E E E E E

Java Code

class Main {
   
   static void pattern16(int N)
{
     
      // Outer loop for the number of rows.
      for(int i=0;i<N;i++){
          
          
          for(int j=0;j<=i;j++){
              
              // same char which is defined 
              // is to be printed i times in that row.
              System.out.print((char)((int)('A'+i)) + " ");
              
          }
          // As soon as the letters for each iteration are printed, we move to the
          // next row and give a line break otherwise all letters
          // would get printed in 1 line.
          System.out.println();
         
      }
}

    public static void main(String[] args) {
        
        // Here, we have taken the value of N as 5.
        // We can also take input from the user.
        int N = 5;
        pattern16(N);
    }
}

Output 


B B
C C C
D D D D
E E E E E

Special thanks to Priyanshi Goel for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this articleIf you want to suggest any improvement/correction in this article please mail us at [email protected]