Web Hosting Talk







View Full Version : C++ Question


CresHost
11-13-2006, 10:30 AM
Hello,

I need a little help . I want a program whose output is :


Enter a positive integer : 5

1
22
333
4444
55555


I know its gotta be done using for loops but can anyone help?

I have made so far:

#include<iostream.h>
#include<conio.h>
void main(void)
{
int x;
cout<<"Enter a positive integer ?";
cin>>x;
for ( int i=1; i<=x; i++ )
{
cout<<i<<endl;
}
getch();
}

hehachris
11-13-2006, 11:06 AM
you need to write a nested for loop
which means a loop inside another loop

for(){
for(){
}
}

6PS-Chris
11-13-2006, 11:07 AM
#include<iostream.h>
#include<conio.h>
void main(void)
{
int x;
cout<<"Enter a positive integer ?";
cin>>x;
for ( int i=1; i<=x; i++ )
{
for(int ii = 1; ii < i; ii++)
{
cout<<i<<endl;
}
}
getch();
}

CresHost
11-13-2006, 11:28 AM
Thanks Chris . It worked , Just had to make a little modification to your code:

#include<iostream.h>
#include<conio.h>
void main(void)
{
int x;
cout<<"Enter a positive integer ?";
cin>>x;
for ( int i=1; i<=x; i++ )
{
cout<<endl;
for(int ii = 1; ii <= i; ii++)
{
cout<<i;
}
}
getch();
}


----


added cout endl in first loop to make it go to next line , and in second loop made it ii<= instead of ii< as it missed 1 otherwise .

Thanks for your help