954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Program Help Using For Nested Loops

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;
}

dontcare
Light Poster
32 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

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;
    }
  }
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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
}
ZuK
Light Poster
29 posts since Oct 2004
Reputation Points: 11
Solved Threads: 0
 

Thanks narue and zuk.

dontcare
Light Poster
32 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You