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:
* *** ***** ******* ********* ********* ******* ***** *** *
Print the pattern in the function given to you.
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Function to print pattern9
static void pattern9(int n) {
erect_pyramid(n);
inverted_pyramid(n);
}
private:
static void erect_pyramid(int n) {
// Outer loop which will loop for the rows.
for (int i = 0; i < n; i++){
// For printing the spaces before stars in each row
for (int j = 0; j < n - i - 1; j++) {
cout<< " ";
}
// For printing the stars in each row
for (int j = 0; j < 2 * i + 1; j++) {
cout<< "*";
}
// For printing the spaces after the stars in each row
for (int j = 0; j < n - i - 1; j++) {
cout<< " ";
}
/* As soon as the stars for each iteration are printed,
we move to the next row and give a line break */
cout << endl;
}
}
static void inverted_pyramid(int n){
// Outer loop which will loop for the rows.
for (int i = 0; i < n; i++){
// For printing the spaces before stars in each row
for (int j =0; j<i; j++){
cout<< " ";
}
// For printing the stars in each row
for(int j=0;j< 2*n -(2*i +1);j++){
cout<< "*";
}
// For printing the spaces after the stars in each row
for (int j =0; j<i; j++){
cout<< " ";
}
/* As soon as the stars for each iteration are printed,
we move to the next row and give a line break */
cout<< endl;
}
}
};
int main() {
int N = 5;
//Create an instance of Solution class
Solution sol;
sol.pattern9(N);
return 0;
}
class Solution {
// Function to print pattern9
public static void pattern9(int n) {
erectPyramid(n);
invertedPyramid(n);
}
private static void erectPyramid(int n) {
// Outer loop which will loop for the rows.
for (int i = 0; i < n; i++) {
// For printing the spaces before stars in each row
for (int j = 0; j < n - i - 1; j++) {
System.out.print(" ");
}
// For printing the stars in each row
for (int j = 0; j < 2 * i + 1; j++) {
System.out.print("*");
}
// For printing the spaces after stars in each row
for (int j = 0; j < n - i - 1; j++) {
System.out.print(" ");
}
/* As soon as the stars for each iteration are printed,
we move to the next row and give a line break */
System.out.println();
}
}
private static void invertedPyramid(int n) {
// Outer loop which will loop for the rows.
for (int i = 0; i < n; i++) {
// For printing the spaces before stars in each row
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
// For printing the stars in each row
for (int j = 0; j < 2 * n - (2 * i + 1); j++) {
System.out.print("*");
}
// For printing the spaces after stars in each row
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
/* As soon as the stars for each iteration are printed,
we move to the next row and give a line break */
System.out.println();
}
}
public static void main(String[] args) {
int N = 5;
// Create an instance of Solution class
Solution sol = new Solution();
sol.pattern9(N);
}
}
class Solution:
def pattern9(self, n):
self.erect_pyramid(n)
self.inverted_pyramid(n)
def erect_pyramid(self, n):
# Outer loop which will loop for the rows.
for i in range(n):
# For printing the spaces before stars in each row
for j in range(n - i - 1):
print(" ", end="")
# For printing the stars in each row
for j in range(2 * i + 1):
print("*", end="")
# For printing the spaces after stars in each row
for j in range(n - i - 1):
print(" ", end="")
# As soon as the stars for each iteration are printed,
# we move to the next row and give a line break
print()
def inverted_pyramid(self, n):
# Outer loop which will loop for the rows.
for i in range(n):
# For printing the spaces before stars in each row
for j in range(i):
print(" ", end="")
# For printing the stars in each row
for j in range(2 * n - (2 * i + 1)):
print("*", end="")
# For printing the spaces after stars in each row
for j in range(i):
print(" ", end="")
""" As soon as the stars for each iteration are printed,
we move to the next row and give a line break"""
print()
if __name__ == "__main__":
N = 5
# Create an instance of Solution class
sol = Solution()
sol.pattern9(N)
class Solution {
// Function to print pattern9
pattern9(n) {
this.erectPyramid(n);
this.invertedPyramid(n);
}
erectPyramid(n) {
// Outer loop which will loop for the rows.
for (let i = 0; i < n; i++) {
// For printing the spaces before stars in each row
for (let j = 0; j < n - i - 1; j++) {
process.stdout.write(" ");
}
// For printing the stars in each row
for (let j = 0; j < 2 * i + 1; j++) {
process.stdout.write("*");
}
// For printing the spaces after stars in each row
for (let j = 0; j < n - i - 1; j++) {
process.stdout.write(" ");
}
/* As soon as the stars for each iteration are printed,
we move to the next row and give a line break */
console.log();
}
}
invertedPyramid(n) {
// Outer loop which will loop for the rows.
for (let i = 0; i < n; i++) {
// For printing the spaces before stars in each row
for (let j = 0; j < i; j++) {
process.stdout.write(" ");
}
// For printing the stars in each row
for (let j = 0; j < 2 * n - (2 * i + 1); j++) {
process.stdout.write("*");
}
// For printing the spaces after stars in each row
for (let j = 0; j < i; j++) {
process.stdout.write(" ");
}
/* As soon as the stars for each iteration are printed,
we move to the next row and give a line break */
console.log();
}
}
}
// Main program
let N = 5;
// Create an instance of Solution class
let sol = new Solution();
sol.pattern9(N);