For loop

Jul 24, 2013 at 7:18am
i want to create a program that displays this exact numbers:
10
109
1098
10987

i want to see the code for this for me to understand loop. i just can't figure out how it is done.
Jul 24, 2013 at 7:37am
It is done the following way

10

10 9

10 98

10 987

So all what you need to do is in fact to display

9
98
987
Jul 24, 2013 at 8:09am
The straitforward approach is for example

1
2
3
4
5
6
for ( size_t i = 0; i < 4; i++ )
{
	std::cout << 10;
	for ( size_t j = 9; j > 9 - i; j-- ) std::cout << j;
	std::cout << std::endl;
}


There are many ways to do the task. For example

1
2
3
4
5
6
std::string s( "10" );

for ( size_t i = 10; i != 6; s += char( --i + '0' ) ) 
{
	std::cout << s << std::endl;
}
Jul 24, 2013 at 12:58pm
This is it in its most basic form.

1
2
3
4
5
6
7
8
9
for (size_t nMin=10; nMin>0; nMin--)
{
    for (size_t nNum=10; nNum>=nMin; nNum--)
    {
        std::cout << nNum;
    }
    std::cout << std::endl;
}
Jul 24, 2013 at 2:44pm
i typed up a guide for loops that breaks down the concepts and each type with example codes. iIf you would like me to email it to you just pm me your email.
Jul 24, 2013 at 5:03pm
Enjoy!:)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

int main()
{
	while ( true )
	{
		std::cout << "Enter a non-negative number (0 - exit): ";

		size_t n;
		std::cin >> n;

		if ( !n ) break;

		std::cout << std::endl;

		for ( size_t i = 0; i < n; i++ )
		{
			const size_t base = 10;

			size_t j = 0;
			do
			{
				std::cout << base - j % base;
			} while ( j++ != i );
			std::cout << std::endl;
		}
	}
}
Jul 31, 2013 at 11:34am
the straightforward approach youve stated is what im looking for. thanks :D
Topic archived. No new replies allowed.