Given an integer n. You need to recreate the pattern given below for any value of N. Let's say for N = 5, the pattern should look like as below:
E
D E
C D E
B C D E
A B C D E
Print the pattern in the function given to you.
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
//Function to print pattern18
void pattern18(int n) {
// Outer loop for the number of rows.
for(int i = 0; i < n; i++) {
/* Inner loop for printing 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<<" ";
}
// Move to the next line for the next row.
cout << endl;
}
}
};
int main() {
int N = 5;
//Create an instance of Solution class
Solution sol;
sol.pattern18(N);
return 0;
}
import java.util.*;
class Solution {
// Function to print pattern18
public void pattern18(int n) {
// Outer loop for the number of rows.
for (int i = 0; i < n; i++) {
/* Inner loop for printing alphabets
from A + n -1 -i (i is row no.) to
A + n -1 ( E in this case).*/
for(char ch = (char)(('A'+ n-1)-i); ch <= ('A'+ n-1); ch++){
System.out.print(ch+" ");
}
// Move to the next line for the next row.
System.out.println();
}
}
public static void main(String[] args) {
int N = 5;
//Create an instance of Solution class
Solution sol = new Solution();
sol.pattern18(N);
}
}
class Solution:
# Function to print pattern18
def pattern18(self, n):
# Outer loop for the number of rows.
for i in range(n):
""" Inner loop for printing alphabets
from 'A' + n - 1 - i to 'A' + n - 1."""
for ch in range(ord('A') + n - 1 - i, ord('A') + n):
print(chr(ch), end=" ")
# Move to the next line for the next row.
print()
if __name__ == "__main__":
N = 5
# Create an instance of Solution class
sol = Solution()
sol.pattern18(N)
class Solution {
// Function to print pattern18
pattern18(n) {
// Outer loop for the number of rows.
for (let i = 0; i < n; i++) {
/* Inner loop for printing alphabets
from 'A' + n - 1 - i to 'A' + n - 1.*/
for (let ch = 'A'.charCodeAt(0) + n - 1 - i; ch <= 'A'.charCodeAt(0) + n - 1; ch++) {
process.stdout.write(String.fromCharCode(ch) + " ");
}
// Move to the next line for the next row.
console.log();
}
}
}
let N = 5;
//Create an instace of Solution class
let sol = new Solution();
sol.pattern18(N);