pattern number with looping

hey... could anyone tell me how to make some pattern like this
1
22
333
4444
55555

thx before...

i've make a code like this

#include <iostream.h>

int main ()
{
int i,j;
for (i=5;i>=1;i--)
{
for (j=1;j<=i;j++)
{
cout << i;
}
cout << endl;
}
}


but what i got is like this

55555
4444
333
22
1

and i want it to be like this

1
22
333
4444
55555

which part of code i need to change??
thx
Last edited on
The purpose of the homework is for you to figure out how looping constructs work. Think about how each part of the loop works. Why is it that the numbers start at five and work down to one? Why are there the same number of digits as the value of the digit (that is, why are there three '3's, or four '4's)?

You need to figure this out to be able to program. Good luck!
1. That code is severely obfuscated. Use [code] [/code] tags either by typing [code] <paste code here> [/code] or by clicking the # under the word Format: on the right hand side of the screen and pasting your code after the [code] tag. See below for the result.

2. iostream.h is deprecated

3. You need to prefix cout with std:: to use it, or put "using namespace std;" at the top of the file, before "int main()"

4. Your line "for (i=5;i>=1;i--)" was doing the opposite of what it should have been doing. I changed it to "for (i = 1; i <= 5; ++i)" and it worked because the way you did it, it was decrementing i, not incrementing it.

5. Main needs to return a number. Place "return 0;" at the end of the function, just before the closing brace ('}').

6. You don't need to use endl; endl prints a newline and then flushes the buffer. You don't need to flush the buffer.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main() {
    int i,j;
    for (i = 1; i <= 5; ++i) {
        for (j = 1; j <= i; ++j) {
            std::cout << i;
        }
        std::cout << "\n";
    }
    return 0;
}
Last edited on
wow... ok... thz for the help....^^
Topic archived. No new replies allowed.