is it possible to get an output like
1
22
333
4444
55555
using for loops?
Printable View
is it possible to get an output like
1
22
333
4444
55555
using for loops?
Yes.
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") ;
>> 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;
}
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.Quote:
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.
this is the solution and i accept any one comment on my codes
Quote:
int n;
cin >> n;
for(int i=1; i<n+1 ;i++)
{cout << endl;
for(int j=1; j<i+1; j++)
cout << i;
}
if im not mistaken this person is not asking this solution for homework but for his/her own stdy
because this same Q i have it in my tutorial
and after one day only we r having final exam << so scared
actually i agree not posting solutions only and only because of cheating
otherwise why not?
maybe some1 will say spoonfeeding
so why not
i agree with spoonfeeding the information and knowlage especially for begging so will have a strong foundation within short time
lets save this time and energy for what no one knows about
>> if im not mistaken this person is not asking this solution for homework but for his/her own stdy <<
Unfortunately there's no way to know that for sure, so the policy is, don't give out solutions at all.
>> i agree with spoonfeeding the information and knowlage <<
Learning to program isn't about learning the correct syntax, it's about learning about how to approach a problem. Providing correct syntax does little to help the person learn how to approach the problem. It also prevents the person from gaining the experience that comes from approaching a problem from the wrong perspective and figuring out what went wrong.
Complete examples don't provide any benefits that walking a person through completion of the problem can't also provide.
just take this code
and turn this in
Code:#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;
int main()
{
char a;
int b=0;
for(a=49;a<54;a++)
{
b++;
cout<<setfill(a)<<setw(b)<<a<<endl;
}
_getch();
}