is it possible to get an output like
1
22
333
4444
55555
using for loops?
is it possible to get an output like
1
22
333
4444
55555
using for loops?
can anyone give a hint or somethin?
please!
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") ;
Mainframe assembler programmer by trade. C coder when I can.
>> can anyone give a hint or somethin?
The condition of the loop doesn't have to use a plain number. So:You don't have to use a number like 10 there. You can use a variable.Code:for (int i = 0; i < 10; ++i)
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; }
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.
Haha! That is amusing.
Example:
Turn that in.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; }
Last edited by master5001; 10-07-2008 at 12:39 PM.
Your loop could have been designed more similarly to my print loop. You are introducing unnecessary variables into your method.
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.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.
>> 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.
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.