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:
ABCDE
ABCD
ABC
AB
A
Print the pattern in the function given to you.
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
//Function to print pattern15
void pattern15(int n) {
// Outer loop for the number of rows.
for(int i=0;i<n;i++){
/*Inner loop will loop for i times and
print alphabets from A to A + (n-i-1).*/
for(char ch = 'A'; ch<='A'+(n-i-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() {
int N = 5;
//Create an instance of Solution class
Solution sol;
sol.pattern15(N);
return 0;
}
class Solution {
// Function to print pattern15
void pattern15(int n) {
// Outer loop for the number of rows.
for (int i = 0; i < n; i++) {
/*Inner loop will loop for i times and
print alphabets from A to A + (n - i - 1).*/
for (char ch = 'A'; ch<='A'+(n-i-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) {
int N = 5;
// Create an instance of Solution class
Solution sol = new Solution();
sol.pattern15(N);
}
}
class Solution:
# Function to print pattern15
def pattern15(self, n):
# Outer loop for the number of rows.
for i in range(n):
"""Inner loop will loop for i times and
print alphabets from A to A + (n - i - 1)."""
for ch in range(ord('A'), ord('A') + n - i):
print(chr(ch), end="")
"""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."""
print()
if __name__ == "__main__":
N = 5
# Create an instance of Solution class
sol = Solution()
sol.pattern15(N)
class Solution {
// Function to print pattern15
pattern15(n) {
// Outer loop for the number of rows.
for (let i = 0; i < n; i++) {
/* Initialize an empty string to
accumulate characters for the current row*/
let row = "";
/* Inner loop will loop for (n - i) times and
print alphabets from 'A' to 'A' + i.*/
for (let j = 0, ch = 'A'; j <= (n - i - 1); j++, ch = String.fromCharCode(ch.charCodeAt(0) + 1)) {
row += ch;
}
/* Print the accumulated row string
for the current iteration*/
console.log(row);
}
}
}
let N = 5;
// Create an instance of Solution class
let sol = new Solution();
sol.pattern15(N);