FOR LOOP

Hello CplusPlus forum.

Im making a simple program using for-loop with this output:

1234
123
12
1

but my code doesnt seem to work.
any help?

Here's my code:
#include<iostream.h>
int x,y=1234;

main()
{
for(x=0;x<10;x++)

cout<<y;
cout<<"\n";
return 0;
}


THANKS!
for ( int x = 1234; x; x /= 10 ) std::cout << x << std::endl;


EDIT: Or if you are using any positive number ( not only 1234) then you can write

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

int main()
{
   unsigned int y = 0;

   std::cout << "Enter a positive number: ";
   std::cin >> y;

   for ( unsigned int x = y; x; x /= 10 ) std::cout << x << std::endl;
}
Last edited on
Your program should change :

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream.h>
int x,y=1234;

main()
{
for(x=0;x<4;x++)
{
                 cout<<y<<endl;
                 y=y/10;
}
return 0;
}


note : if you need any explanation, tell me:)
Thanks for your replys Vlad and Rapunzel

I've got it. and added some extras :D
Topic archived. No new replies allowed.