I'm having trouble with this program the objective is to Prompt the for a int. from 0-999, then split the int into three digits, then output the three digits from 000 through number entered using 3 nested for loops. I can get the second and last digits to work but I can't figure out what if statement(s) I need to use, so the program will work. Here is what I have so far:
int main()
{
int number, c, d, e;
cout << "Enter a Positive Integer from 0 to 999." << endl;
cin >> number;
while ((number < 0) || (number > 999))
{
cout << "Error: Negative Number or Number Greater than 999\n" << "Reenter that number and continue: \n";
cin >> number; }

c = number / 100;
d = (number % 100 - number % 10) / 10;
e = number % 10;

for ( int k= 0 ; k <= c ; k++)

for (int i = 0; i <= d ; i++)

for ( int j= 0; j <= e ; j++)

cout << k << i << j << endl;

return 0;
}

Recommended Answers

All 3 Replies

Consider your boundary conditions. For each digit, unless the next outer loop is on the last iteration, you want to print 0 through 9. Otherwise you print 0 through the digit value. There isn't a very clean solution; you either use conditional expressions or long and redundant if sequences:

#include <iostream> using namespace std; int main() { int a, b, c; int number; cout<<"Enter a number from 0 - 999: "; cin>> number; a = number / 100; b = (number % 100 - number % 10) / 10; c = number % 10; for ( int i = 0; i <= a; i++ ) { for ( int j = 0; j <= ( ( i == a ) ? b : 9 ); j++ ) { for ( int k = 0; k <= ( ( j == b ) ? c : 9 ); k++ )
        cout<< i << j << k <<endl; } } }

is that what you want ?

#include <iostream> using namespace std; int main() { int number, c, d, e; cout << "Enter a Positive Integer from 0 to 999." << endl; cin >> number; while ((number < 0) || (number > 999)) { cout << "Error: Negative Number or Number Greater than 999\n" << "Reenter that number and continue: \n"; cin >> number; } c = number / 100; d = (number % 100 - number % 10) / 10; e = number % 10; cout << "c=" << c << "d=" << d << "e=" << e << endl; int k,i,j; for ( k= 0 ; k <= 9 ; k++) for (i = 0; i <= 9 ; i++) 
        for ( j= 0; j <= 9 ; j++) { 
        cout << k << i << j << endl;
        if ( k == c && i == d && j == e )
        return 0;
        } return 1; // to keep the compiler happy }

Thanks narue and zuk.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.