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:
12345
1234
123
12
1
Print the pattern in the function given to you.
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Function to print pattern6
static void pattern6(int n) {
// Outer loop which will loop for the rows.
for (int i = 0; i < n; i++) {
// Inner loop which loops for the columns.
for (int j = 0; j < n-i; j++) {
cout << j+1;
}
/* 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.pattern6(N);
return 0;
}
class Solution {
// Function to print pattern6
public static void pattern6(int n) {
// Outer loop which will loop for the rows.
for (int i = 0; i < n; i++) {
// Inner loop which loops for the columns.
for (int j = 0; j < n-i; j++) {
System.out.print(j+1);
}
/* 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.pattern6(N);
}
}
class Solution:
# Function to print pattern6
def pattern6(self, n):
# Outer loop will run for rows.
for i in range(0,n):
# Inner loop will run for columns.
for j in range(0,n-i):
print(j+1, 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.pattern6(N)
if __name__ == "__main__":
Solution().main()
class Solution {
//Function to print pattern6
pattern6(n) {
//Outer loop which will loop for the rows.
for (let i = 0; i < n; i++) {
// Inner loop will run for columns.
for (let j = 0; j < n-i; j++) {
process.stdout.write((j+1).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.pattern6(N);