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
12
123
1234
12345
Print the pattern in the function given to you.
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Function to print pattern3
static void pattern3(int n) {
// Outer loop which will loop for the rows.
for (int i = 1; i <= n; i++) {
// Inner loop which loops for the columns.
for (int j = 1; j <= i; j++) {
cout << j;
}
/* As soon as stars for each iteration are printed,
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.pattern3(N);
return 0;
}
class Solution {
// Function to print pattern3
public static void pattern3(int n) {
// Outer loop which will loop for the rows.
for (int i = 1; i <= n; i++) {
// Inner loop which loops for the columns.
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
/* As soon as stars for each iteration are printed,
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.pattern3(N);
}
}
class Solution:
# Function to print pattern3
def pattern3(self, n):
# Outer loop will run for rows.
for i in range(1,n+1):
# Inner loop will run for columns.
for j in range(1,i+1):
print(j, end="")
""" As soon as n stars are printed, move
to the next row and give a line break."""
print()
def main(self):
N = 5
# Create an instance of the Solution class
sol = Solution()
sol.pattern3(N)
if __name__ == "__main__":
Solution().main()
class Solution {
//Function to print pattern3
pattern3(n) {
//Outer loop which will loop for the rows.
for (let i = 1; i <= n; i++) {
// Inner loop will run for columns.
for (let j = 1; j <= i; j++) {
process.stdout.write(j.toString());
}
/* As soon as n stars are printed, move
to the next row and give a line break. */
console.log();
}
}
}
const N = 5;
// Create an instance of the Solution class
const sol = new Solution();
sol.pattern3(N);