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 pattern10
void pattern10(int n) {
// Outer loop for number of rows.
for (int i = 1; i <= 2 * n - 1; i++) {
/* stars would be equal to the
row no. uptill first half */
int stars = i;
// for the second half of rotated triangle.
if (i > n) stars = 2 * n - i;
// for printing the stars in each row.
for (int j = 1; j <= stars; j++) {
cout << "*";
}
/* As soon as the stars for each iteration are printed,
we 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.pattern10(N);
return 0;
}
class Solution {
// Function to print pattern10
public static void pattern10(int n) {
// Outer loop for number of rows.
for (int i = 1; i <= 2 * n - 1; i++) {
/* stars would be equal to the
row no. uptill first half */
int stars = i;
// for the second half of rotated triangle.
if (i > n) stars = 2 * n - i;
// for printing the stars in each row.
for (int j = 1; j <= stars; j++) {
System.out.print("*");
}
/* As soon as the stars for each iteration are printed,
we 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.pattern10(N);
}
}
class Solution:
#Function to print pattern10
def pattern10(self, n):
# Outer loop for number of rows.
for i in range(1, 2 * n):
""" stars would be equal to the
row no. uptill first half"""
stars = i if i <= n else 2 * n - i
# for printing the stars in each row.
for j in range(1, stars + 1):
print("*", end="")
""" As soon as the stars for each iteration are
printed, we move to the next row and give a line break"""
print()
if __name__ == "__main__":
N = 5
# Create an instance of Solution class
sol = Solution()
sol.pattern10(N)
class Solution {
//Function to print pattern10
pattern10(n) {
// Outer loop for number of rows.
for (let i = 1; i <= 2 * n - 1; i++) {
/* stars would be equal to the
row no. uptill first half*/
let stars = i <= n ? i : 2 * n - i;
// for printing the stars in each row.
for (let j = 1; j <= stars; j++) {
process.stdout.write("*");
}
/* As soon as the stars for each iteration are printed,
we move to the next row and give a line break*/
console.log();
}
}
}
let N = 5;
//Create an instance of Solution class
let sol = new Solution();
sol.pattern10(N);