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 pattern20
void pattern20(int n) {
// Initialising the spaces.
int spaces = 2*n-2;
// Outer loop to print the row.
for(int i = 1; i <= 2*n-1; i++){
// Stars for first half
int stars = i;
// Stars for the second half.
if(i > n) stars = 2*n - i;
//For printing the stars
for(int j = 1; j <= stars; j++){
cout<<"*";
}
//For printing the spaces
for(int j = 1; j <= spaces; j++){
cout<<" ";
}
//For printing the stars
for(int j = 1; j <= stars; j++){
cout<<"*";
}
//Give a line break for new row.
cout<<endl;
if(i<n) spaces -=2;
else spaces +=2;
}
}
};
int main() {
int N = 5;
//Create an instance of Solution class
Solution sol;
sol.pattern20(N);
return 0;
}
import java.util.*;
class Solution {
// Function to print pattern20
public void pattern20(int n) {
// Initialising the spaces.
int spaces = 2*n-2;
// Outer loop to print the row.
for(int i = 1; i <= 2*n-1; i++){
// Stars for first half
int stars = i;
// Stars for the second half.
if(i > n) stars = 2*n - i;
// For printing the stars
for(int j = 1; j <= stars; j++){
System.out.print("*");
}
// For printing the spaces
for(int j = 1; j <= spaces; j++){
System.out.print(" ");
}
// For printing the stars
for(int j = 1; j <= stars; j++){
System.out.print("*");
}
// Give a line break for new row.
System.out.println();
if(i < n) spaces -= 2;
else spaces += 2;
}
}
public static void main(String[] args) {
int N = 5;
// Create an instance of Solution class
Solution sol = new Solution();
sol.pattern20(N);
}
}
class Solution:
# Function to print pattern20
def pattern20(self, n):
# Initialising the spaces.
spaces = 2 * n - 2
# Outer loop to print the row.
for i in range(1, 2 * n):
# Stars for first half
stars = i
# Stars for the second half.
if i > n:
stars = 2 * n - i
# For printing the stars
print("*" * stars, end="")
# For printing the spaces
print(" " * spaces, end="")
# For printing the stars
print("*" * stars, end="")
# Give a line break for new row.
print()
# Adjust spaces for the next row
if i < n:
spaces -= 2
else:
spaces += 2
# Main function
if __name__ == "__main__":
N = 5
# Create an instance of Solution class
sol = Solution()
sol.pattern20(N)
class Solution {
// Function to print pattern20
pattern20(n) {
// Initialising the spaces.
let spaces = 2 * n - 2;
// Outer loop to print the row.
for(let i = 1; i <= 2 * n - 1; i++){
// Stars for first half
let stars = i;
// Stars for the second half.
if(i > n) stars = 2 * n - i;
// For printing the stars
process.stdout.write("*".repeat(stars));
// For printing the spaces
process.stdout.write(" ".repeat(spaces));
// For printing the stars
process.stdout.write("*".repeat(stars));
// Give a line break for new row.
console.log();
if(i < n) spaces -= 2;
else spaces += 2;
}
}
}
let N = 5;
//Create an instance of Solution class
let sol = new Solution();
sol.pattern20(N);