Write a program to print a symmetric star pattern with exactly 10 rows as specified. The pattern has stars and leading spaces arranged such that the number of stars per row from top to bottom is: 1, 3, 5, 7, 9, 9, 7, 5, 3, 1. The corresponding leading spaces for each row are: 8, 7, 6, 5, 4, 4, 5, 6, 7, 8. The output must match the pattern exactly without any additional characters or deviations.
Input: None
Output:
*
***
*****
*******
*********
*********
*******
*****
***
*
Input: None
Output:
*
***
*****
*******
*********
*********
*******
*****
***
*
Input: None
Output:
*
***
*****
*******
*********
*********
*******
*****
***
*
The pattern is symmetric with exactly 10 rows. The first 5 rows have an increasing odd number of stars (1, 3, 5, 7, 9) and decreasing leading spaces (8, 7, 6, 5, 4). The next 5 rows mirror this in reverse: stars decrease (9, 7, 5, 3, 1) and spaces increase (4, 5, 6, 7, 8).
#include <iostream>
#include <string>
using namespace std;
int main() {
for (int i = 0; i < 10; i++) {
if (i < 5) {
cout << string(8 - i, ' ') << string(2*i + 1, '*') << endl;
} else {
cout << string(i - 1, ' ') << string(2*(9-i) + 1, '*') << endl;
}
}
return 0;
}
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i < 5) {
System.out.println(" ".repeat(8 - i) + "*".repeat(2*i + 1));
} else {
System.out.println(" ".repeat(i - 1) + "*".repeat(2*(9-i) + 1));
}
}
}
}
for i in range(10):
if i < 5:
print(' ' * (8 - i) + '*' * (2*i + 1))
else:
print(' ' * (i - 1) + '*' * (2*(9-i) + 1))
for (let i = 0; i < 10; i++) {
if (i < 5) {
console.log(' '.repeat(8 - i) + '*'.repeat(2*i + 1));
} else {
console.log(' '.repeat(i - 1) + '*'.repeat(2*(9-i) + 1));
}
}
Time Complexity: O(1), because the loop runs exactly 10 times and each iteration does a constant amount of work (printing a fixed maximum of 17 characters per row).
Space Complexity: O(1), as we only use a few integer variables and the strings built are of constant maximum length (at most 9 stars and 8 spaces, total 17 characters).
Your notes are automatically saved in your browser's local storage and will persist across sessions on this device.