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

Here, N = 5.
Examples:
Input Format: N = 3 Result: C B C A B C Input Format: N = 6 Result: F E F D E F C D E F B C D E F A B C D E F
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 alpha triangle as shown in the examples above. We observe from the examples that each row ends with the letter E in the case when N = 5 ( ‘A’ + 4 ). Also the triangle has to be right-angled so like the previous patterns, the outer loop will run for N times and the inner loop for i times. In the inner loop, we’ll start from the letter that comes i before the (‘A’ + N -1)th letter and then run the loop till we reach (‘A’ + N-1) in every row. For example, for N = 5 in each row, the letters will be printed from ‘E’ – i to ‘E’ where i is the row index.
Code:
C++ Code
#include <bits/stdc++.h>
using namespace std;
void pattern18(int N)
{
// Outer loop for the no. of rows.
for(int i=0;i<N;i++){
// Inner loop for printing the alphabets from
// A + N -1 -i (i is row no.) to A + N -1 ( E in this case).
for(char ch =('A'+N-1)-i;ch<=('A'+N-1);ch++){
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;
pattern18(N);
return 0;
}
Output
E
D E
C D E
B C D E
A B C D E
Java Code
class Main {
static void pattern18(int N)
{
// Outer loop for the no. of rows.
for(int i=0;i<N;i++){
// Inner loop for printing the alphabets from
// A + N -1 -i (i is row no.) to A + N -1 ( E in this case).
for(char ch =(char)(int)('A'+N-1-i);ch<=(char)(int)('A'+N-1);ch++){
System.out.print(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.
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;
pattern18(N);
}
}
Output
E
D E
C D E
B C D E
A B C D 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 article. If you want to suggest any improvement/correction in this article please mail us at [email protected]