Need help with this program stars;

I got the program to do 10 lines of stars and increment by 1 until 5 and then decrement by 1 from 5 to 1. Now i want to increment the star on each line by 2 and have 1 star on the first line and then 3 one the second... until the 5th where there would be 9 and then decrement by 2 going from 9 to 1 but i cant seem to figure it out. This is the code i have but it doesnt work. Help please

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#include <iostream>

using namespace std;
int main () {
int i,j;
for (i=1; i<=5; ++i) {
        for (j=1; j<=i; j+=2){
        cout << "*"; }
        cout << "\n";
        }
for (i=5; i>=1; --i) {
        for (j=1; j<=i; j+=2) {
        cout << "*";}
        cout << endl;
        }
        return 0;
}
1
2
for (j=1; j<=i; j+=2){
        cout << "*"; }


Is the problem and likewise for the second loop. What you could do is this

1
2
3
4
5
6
7
8
9
 
int z = 1; 
for (int i = 1; i <=5; i++) { 
     for (int j = 1; j <= z; j++) {
          cout << "*";
      }
      z = z +2; 
      cout << "\n"; 
}


You have to alter this a bit when descending, but, it wouldn't be to much work.
Last edited on
Topic archived. No new replies allowed.