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
BB
CCC
DDDD
EEEEE
Print the pattern in the function given to you.
variable to store the alphabet that needs to be printed in each row, depending upon the row numbers.
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
//Function to print pattern16
void pattern16(int n) {
// Outer loop for the number of rows.
for(int i=0;i<n;i++){
// Defining character for each row.
char ch = 'A'+i;
for(int j=0;j<=i;j++){
/*same char is to be printed
i times in that row.*/
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.pattern16(N);
return 0;
}
class Solution {
// Function to print pattern16
void pattern16(int n) {
// Outer loop for the number of rows.
for (int i = 0; i < n; i++) {
// Defining character for each row.
char ch = (char) ('A' + i);
for (int j = 0; j <= i; j++) {
/* same char is to be printed
i times in that row.*/
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.pattern16(N);
}
}
class Solution:
# Function to print pattern16
def pattern16(self, n):
# Outer loop for the number of rows.
for i in range(n):
# Defining character for each row.
ch = chr(ord('A') + i)
for j in range(i + 1):
"""same char is to be printed
i times in that row."""
print(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()
N = 5
# Create an instance of Solution class
sol = Solution()
sol.pattern16(N)
class Solution {
// Function to print pattern16
pattern16(n) {
// Outer loop for the number of rows.
for (let i = 0; i < n; i++) {
// Defining character for each row.
let ch = String.fromCharCode('A'.charCodeAt(0) + i);
for (let j = 0; j <= i; j++) {
/* same char is to be printed
i times in that row.*/
process.stdout.write(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. */
console.log();
}
}
}
let N = 5;
// Create an instance of Solution class
let sol = new Solution();
sol.pattern16(N);