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