Help

Dec 8, 2013 at 7:47pm

Can you please help me with this pattern ?

1
2
3
4
5
6
7
8
9
1
22
3**
4444
5****
666666
7******
88888888
9********
Last edited on Dec 8, 2013 at 8:45pm
Dec 8, 2013 at 9:40pm
what I understood was that in a given line 'x' you output 'x' times the number of that line if the line is even otherwise you output the number of the line and (x-1) asterisks
Last edited on Dec 8, 2013 at 9:41pm
Dec 8, 2013 at 9:54pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

int main()
{
    int number;

    std::cout << "Enter a maximum number: ";
    std::cin >> number;

    for (int i = 0 ; i <= number ; i++){
        if (i%2 == 0){
            for (int j = 0 ; j < i ; j++){
                std::cout << i;
            }
        }

        else {
            std::cout << i;
            for (int j = 0 ; j < (i - 1) ; j++){
                std::cout << "*";
            }
        }
        std::cout << "\n";
    }
    return 0;
}
Dec 8, 2013 at 10:08pm
read another question on the site, guess you could do it this way also ;p

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

int main()
{
    int number;

    std::cout << "Enter a maximum number: ";
    std::cin >> number;

    for (int i = 0 ; i <= number ; i++){

        if (i%2){
            std::cout << i;
            for (int j = 0 ; j < (i - 1) ; j++){
                std::cout << "*";
            }
        }
        else{
            for (int j = 0 ; j < i ; j++){
                std::cout << i;
            }
        }

        std::cout << "\n";
    }
    return 0;
}
Dec 8, 2013 at 11:20pm
Thank you very much guys.
Topic archived. No new replies allowed.