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:
1 1 12 21 123 321 1234 4321 1234554321
Print the pattern in the function given to you.
spaces
. #include <bits/stdc++.h>
using namespace std;
class Solution {
public:
//Function to print pattern12
void pattern12(int n) {
// Initial no. of spaces in row 1.
int spaces = 2*(n-1);
// Outer loop for the number of rows.
for(int i=1;i<=n;i++){
// For printing numbers in each row
for(int j=1;j<=i;j++){
cout<<j;
}
// For printing spaces in each row
for(int j = 1;j<=spaces;j++){
cout<<" ";
}
// For printing numbers in each row
for(int j=i;j>=1;j--){
cout<<j;
}
/* As soon as the numbers for each iteration
are printed, we move to the next row and give
a line break otherwise all numbers would get
printed in 1 line*/
cout<<endl;
/* After each iteration nos. increase by
2, thus spaces will decrement by 2*/
spaces-=2;
}
}
};
int main() {
int N = 5;
//Create an instance of Solution class
Solution sol;
sol.pattern12(N);
return 0;
}
class Solution {
// Function to print pattern12
void pattern12(int n) {
// Initial no. of spaces in row 1.
int spaces = 2 * (n - 1);
// Outer loop for the number of rows.
for (int i = 1; i <= n; i++) {
// For printing numbers in each row
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
// For printing spaces in each row
for (int j = 1; j <= spaces; j++) {
System.out.print(" ");
}
// For printing numbers in each row
for (int j = i; j >= 1; j--) {
System.out.print(j);
}
/* As soon as the numbers for each iteration
are printed, we move to the next row and give
a line break otherwise all numbers would get
printed in 1 line*/
System.out.println();
/* After each iteration nos. increase by
2, thus spaces will decrement by 2*/
spaces -= 2;
}
}
public static void main(String[] args) {
int N = 5;
// Create an instance of Solution class
Solution sol = new Solution();
sol.pattern12(N);
}
}
class Solution:
# Function to print pattern12
def pattern12(self, n):
# Initial no. of spaces in row 1.
spaces = 2 * (n - 1)
# Outer loop for the number of rows.
for i in range(1, n + 1):
# For printing numbers in each row
for j in range(1, i + 1):
print(j, end="")
# For printing spaces in each row
for j in range(1, spaces + 1):
print(" ", end="")
# For printing numbers in each row
for j in range(i, 0, -1):
print(j, end="")
""" As soon as the numbers for each iteration
are printed, we move to the next row and give
a line break otherwise all numbers would get
printed in 1 line"""
print()
""" After each iteration nos. increase by
2, thus spaces will decrement by 2"""
spaces -= 2
if __name__ == "__main__":
N = 5
# Create an instance of Solution class
sol = Solution()
sol.pattern12(N)
class Solution {
// Function to print pattern12
pattern12(n) {
// Initial no. of spaces in row 1.
let spaces = 2 * (n - 1);
// Outer loop for the number of rows.
for (let i = 1; i <= n; i++) {
// For printing numbers in each row
for (let j = 1; j <= i; j++) {
process.stdout.write(j.toString());
}
// For printing spaces in each row
for (let j = 1; j <= spaces; j++) {
process.stdout.write(" ");
}
// For printing numbers in each row
for (let j = i; j >= 1; j--) {
process.stdout.write(j.toString());
}
/* As soon as the numbers for each iteration
are printed, we move to the next row and give
a line break otherwise all numbers would get
printed in 1 line */
console.log();
/* After each iteration nos. increase by
2, thus spaces will decrement by 2 */
spaces -= 2;
}
}
}
// Main program
let N = 5;
// Create an instance of Solution class
let sol = new Solution();
sol.pattern12(N);