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 pattern5
static void pattern5(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 << "*";
}
/* 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.pattern5(N);
return 0;
}
class Solution {
// Function to print pattern5
public static void pattern5(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("*");
}
/* 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.pattern5(N);
}
}
class Solution:
# Function to print pattern5
def pattern5(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("*", 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.pattern5(N)
if __name__ == "__main__":
Solution().main()
class Solution {
//Function to print pattern5
pattern5(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("*");
}
/* 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.pattern5(N);