| | |
Program Help Using For Nested Loops
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2004
Posts: 32
Reputation:
Solved Threads: 0
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;
}
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;
}
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:
C++ Syntax (Toggle Plain Text)
#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; } } }
I'm here to prove you wrong.
•
•
Join Date: Oct 2004
Posts: 29
Reputation:
Solved Threads: 0
is that what you want ?
C++ Syntax (Toggle Plain Text)
#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 }
![]() |
Similar Threads
- Help with Nested Loops, C (C)
- Problem in understandting Multi dimensional array and nested loops (C)
- i need help! using nested for loops (C++)
- A question about nested loops (C++)
- Nested For Loops (C++)
Other Threads in the C++ Forum
- Previous Thread: Hello Everyone
- Next Thread: how to include mhash library in turbo c++
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






