# Pattern – 17: Alpha-Hill Pattern

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

Here, N = 4.

Examples:

```Input Format: N = 3
Result:
A
ABA
ABCBA

Input Format: N = 6
Result:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
```

### Solution

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

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.

In this problem, we have to print an alphabet triangle as shown in the examples above where we can clearly observe that the first row has only the letter A in the middle and some spaces on either side of it and the second row has the letters ABA and some spaces again on both sides. So, we observe from this that first there are some spaces, then letters increasing from A to A + i where i is the row number and then decreasing back to A, then finally some more spaces to the end. Hence, like all the previous patterns the outer loop will loop for N times and there will be three inner loops in this pattern.

The first inner loop is for printing the (N-i-1) spaces ( 1st row -> 4 spaces, 2nd row -> 3 spaces, and so on), the second one for printing the characters, and then the last one again for printing the spaces. For the character loop, we define the breakpoint till where the character must increase and after that, it must decrease. The breakpoint can be defined by (2*i+1)/2 for each row.

Code

## C++ Code

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

void pattern17(int N)
{

// Outer loop for the number of rows.
for(int i=0;i<N;i++){

// for printing the spaces.
for(int j=0;j<N-i-1;j++){
cout<<" ";
}

// for printing the characters.
char ch = 'A';
int breakpoint = (2*i+1)/2;
for(int j=1;j<=2*i+1;j++){

cout<<ch;
if(j <= breakpoint) ch++;
else ch--;
}

// for printing the spaces again after characters.
for(int j=0;j<N-i-1;j++){
cout<<" ";
}

// 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;

pattern17(N);

return 0;
}
``````

Output

A
ABA
ABCBA
ABCDCBA
ABCDEDCBA

## Java Code

``````class Main {

static void pattern17(int N)
{

// Outer loop for the number of rows.
for(int i=0;i<N;i++){

// for printing the spaces.
for(int j=0;j<N-i-1;j++){
System.out.print(" ");
}

// for printing the characters.
char ch = 'A';
int breakpoint = (2*i+1)/2;
for(int j=1;j<=2*i+1;j++){

System.out.print(ch);
if(j <= breakpoint) ch++;
else ch--;
}

// for printing the spaces again.
for(int j=0;j<N-i-1;j++){
System.out.print(" ");
}
// 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;
pattern17(N);
}
}
``````

Output

A

ABA

ABCBA

ABCDCBA

ABCDEDCBA