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 ABA ABCBA ABCDCBA ABCDEDCBA
Print the pattern in the function given to you.
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
//Function to print pattern17
void pattern17(int n) {
// Outer loop for the number of rows.
for(int i = 0; i < n; i++) {
// Printing spaces before characters.
for(int j = 0; j < n - i - 1; j++) {
cout << " ";
}
// Printing characters.
char ch = 'A';
int breakpoint = (2 * i + 1) / 2;
for(int j = 1; j <= 2 * i + 1; j++) {
cout << ch;
if(j <= breakpoint)
ch++;
else
ch--;
}
// Printing spaces after characters.
for(int j = 0; j < n - i - 1; j++) {
cout << " ";
}
// 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.pattern17(N);
return 0;
}
import java.util.*;
class Solution {
// Function to print pattern17
public void pattern17(int n) {
// Outer loop for the number of rows.
for (int i = 0; i < n; i++) {
// Printing spaces before characters.
for (int j = 0; j < n - i - 1; j++) {
System.out.print(" ");
}
// Printing characters.
char ch = 'A';
int breakpoint = (2 * i + 1) / 2;
for (int j = 1; j <= 2 * i + 1; j++) {
System.out.print(ch);
if (j <= breakpoint)
ch++;
else
ch--;
}
// Printing spaces after characters.
for (int j = 0; j < n - i - 1; j++) {
System.out.print(" ");
}
// 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.pattern17(N);
}
}
class Solution:
# Function to print pattern17
def pattern17(self, n):
# Outer loop for the number of rows.
for i in range(n):
# Printing spaces before characters.
for j in range(n - i - 1):
print(" ", end="")
# Printing characters.
ch = 'A'
breakpoint = (2 * i + 1) // 2
for j in range(1, 2 * i + 2):
print(ch, end="")
if j <= breakpoint:
ch = chr(ord(ch) + 1)
else:
ch = chr(ord(ch) - 1)
# Printing spaces after characters.
for j in range(n - i - 1):
print(" ", 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.pattern17(N)
class Solution {
// Function to print pattern17
pattern17(n) {
// Outer loop for the number of rows.
for (let i = 0; i < n; i++) {
// Printing spaces before characters.
for (let j = 0; j < n - i - 1; j++) {
process.stdout.write(" ");
}
// Printing characters.
let ch = 'A';
let breakpoint = Math.floor((2 * i + 1) / 2);
for (let j = 1; j <= 2 * i + 1; j++) {
process.stdout.write(ch);
if (j <= breakpoint)
ch = String.fromCharCode(ch.charCodeAt(0) + 1);
else
ch = String.fromCharCode(ch.charCodeAt(0) - 1);
}
// Printing spaces after characters.
for (let j = 0; j < n - i - 1; j++) {
process.stdout.write(" ");
}
// Move to the next line for the next row.
console.log();
}
}
}
let N = 5;
//Create an instance of Solution class
let sol = new Solution();
sol.pattern17(N);