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
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Print the pattern in the function given to you.
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
//Function to print pattern13
void pattern13(int n) {
// starting the number
int num =1;
// Outer loop for the number of rows.
for(int i=1;i<=n;i++){
/*Inner loop will loop for i times and
print numbers increasing by 1 each time*/
for(int j=1;j<=i;j++){
cout<<num<<" ";
num =num+1;
}
/* 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;
}
}
};
int main() {
int N = 5;
//Create an instance of Solution class
Solution sol;
sol.pattern13(N);
return 0;
}
class Solution {
// Function to print pattern13
void pattern13(int n) {
// starting the number
int num = 1;
// Outer loop for the number of rows.
for (int i = 1; i <= n; i++) {
/*Inner loop will loop for i times and
print numbers increasing by 1 each time*/
for (int j = 1; j <= i; j++) {
System.out.print(num + " ");
num = num + 1;
}
/* 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();
}
}
public static void main(String[] args) {
int N = 5;
// Create an instance of Solution class
Solution sol = new Solution();
sol.pattern13(N);
}
}
class Solution:
# Function to print pattern13
def pattern13(self, n):
# starting the number
num = 1
# Outer loop for the number of rows.
for i in range(1, n + 1):
""" Inner loop will loop for i times and
print numbers increasing by 1 each time"""
for j in range(1, i + 1):
print(num, end=" ")
num += 1
""" 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()
if __name__ == "__main__":
N = 5
# Create an instance of Solution class
sol = Solution()
sol.pattern13(N)
class Solution {
// Function to print pattern13
pattern13(n) {
// starting the number
let num = 1;
// Outer loop for the number of rows.
for (let i = 1; i <= n; i++) {
/*Inner loop will loop for i times and
print numbers increasing by 1 each time*/
for (let j = 1; j <= i; j++) {
process.stdout.write(num + " ");
num++;
}
/* 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();
}
}
}
let N = 5;
// Create an instance of Solution class
let sol = new Solution();
sol.pattern13(N);