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 pattern21
void pattern21(int n) {
// Outer loop for the rows.
for(int i = 0; i < n; i++){
/* Inner loop for printing
the stars at borders only.*/
for(int j = 0; j < n; j++){
if(i == 0 || j == 0 || i == n-1 || j == n-1)
cout<<"*";
/* If it is not border
index, print space.*/
else cout<<" ";
}
//move to the next row
cout<<endl;
}
}
};
int main() {
int N = 5;
//Create an instance of Solution class
Solution sol;
sol.pattern21(N);
return 0;
}
import java.util.*;
class Solution {
// Function to print pattern21
public void pattern21(int n) {
// Outer loop for the rows.
for(int i = 0; i < n; i++){
/* Inner loop for printing
the stars at borders only.*/
for(int j = 0; j < n; j++){
if(i == 0 || j == 0 || i == n-1 || j == n-1)
System.out.print("*");
else
System.out.print(" ");
}
// Move to 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.pattern21(N);
}
}
class Solution:
# Function to print pattern21
def pattern21(self, n):
# Outer loop for the rows.
for i in range(n):
""" Inner loop for printing
the stars at borders only."""
for j in range(n):
if i == 0 or j == 0 or i == n-1 or j == n-1:
print("*", end="")
else:
print(" ", end="")
# Move to the next row.
print()
if __name__ == "__main__":
N = 5
# Create an instance of Solution class
sol = Solution()
sol.pattern21(N)
class Solution {
// Function to print pattern21
pattern21(n) {
// Outer loop for the rows.
for(let i = 0; i < n; i++){
/* Inner loop for printing
the stars at borders only.*/
for(let j = 0; j < n; j++){
if(i === 0 || j === 0 || i === n-1 || j === n-1)
process.stdout.write("*");
else
process.stdout.write(" ");
}
// Move to the next row.
console.log();
}
}
}
let N = 5;
//Create an instance of Solution class
let sol = new Solution();
sol.pattern21(N);