# Thread: use of "for loops"

1. ## use of "for loops"

is it possible to get an output like
1
22
333
4444
55555
using for loops?

2. Yes.

3. can anyone give a hint or somethin?

4. Sure, turn this in:

Code:
```int i ;
for (i = 0 ; i < 1 ; i++) printf("1") ;
for (i = 0 ; i < 1 ; i++) printf("22") ;
for (i = 0 ; i < 1 ; i++) printf("333") ;
for (i = 0 ; i < 1 ; i++) printf("4444") ;
for (i = 0 ; i < 1 ; i++) printf("55555") ;```

5. >> can anyone give a hint or somethin?

The condition of the loop doesn't have to use a plain number. So:
Code:
`for (int i = 0; i < 10; ++i)`
You don't have to use a number like 10 there. You can use a variable.

6. Hi

Yes that is possible, here is an example:
Code:
```int i = 1;
for (int n = 0; n < 5; n++)
{
for (int x = 0; x < i; x++)
{
cout << i;
}
i++;
cout << endl;
}```

7. Please don't post solutions. The OP learns nothing this way. Worse still if it's a school assignment.

Even then, I think there are "cleaner" ways to do it than you have shown.

8. Originally Posted by Dino
Sure, turn this in:

Code:
```int i ;
for (i = 0 ; i < 1 ; i++) printf("1") ;
for (i = 0 ; i < 1 ; i++) printf("22") ;
for (i = 0 ; i < 1 ; i++) printf("333") ;
for (i = 0 ; i < 1 ; i++) printf("4444") ;
for (i = 0 ; i < 1 ; i++) printf("55555") ;```
Haha! That is amusing.

Example:
Code:
```/* Copyright (c) 2008. master5001. All rights reserved. */
#include <vector>
#include <iostream>

int main(void)
{
std::vector<int> a;
std::vector<int>::iterator begin, end;
int i, j, k;

std::cout << "Put a number! "; // no time for pleasantries in this program.
std::cin >> j;

j = +j;

for(int i = 0; i < j;)
a.push_back(++i);

for(begin = a.begin(), end = a.end(); begin != end; begin++, --k)
{
for(i = 0; i < *begin; ++i)
std::cout << *begin;
std::cout << std::endl;
}

return 0;
}```
Turn that in.

9. Originally Posted by cyberfish
Please don't post solutions. The OP learns nothing this way. Worse still if it's a school assignment.

Even then, I think there are "cleaner" ways to do it than you have shown.
Or take my approach and make people scratch their heads and wonder "Why did you do it that way, you moron..."

10. Originally Posted by cyberfish
Please don't post solutions. The OP learns nothing this way. Worse still if it's a school assignment.

Even then, I think there are "cleaner" ways to do it than you have shown.
yes you are right, I'll keep that in mind

11. Your loop could have been designed more similarly to my print loop. You are introducing unnecessary variables into your method.

12. Originally Posted by cyberfish
Please don't post solutions. The OP learns nothing this way. Worse still if it's a school assignment.

Even then, I think there are "cleaner" ways to do it than you have shown.
Why not? Some people like me actually learn by example. Its not that everyone wants you to do their work for them just give them a basic example.

-DenKain

13. Why not? Some people like me actually learn by example. Its not that everyone wants you to do their work for them just give them a basic example.
If the OP posted what he has been trying, we will help him with that. It's not whether you can follow a solution and say "it makes sense". But rather whether you can come up with it in the first place. And then there's the homework assignment issue... I'm sure no professor/instructor wants their student to post their homework assignment on a forum and copy a solution from there.

14. >> Why not?

Because it's cheating. Learning by example is fine if it is not an example of a solution to homework. There's no surefire way to distinguish between homework assignments and people learning on their own, so the result is a policy to not provide solutions to homework questions.

15. A forum is an interactive medium. Examples can be found on static media such as tutorial web-sites and books. I will hush now before people point out how often I post examples... hell including this time.