Program Help Using For Nested Loops

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 32
Reputation: dontcare is an unknown quantity at this point 
Solved Threads: 0
dontcare dontcare is offline Offline
Light Poster

Program Help Using For Nested Loops

 
0
  #1
Oct 3rd, 2004
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;
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,624
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 714
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Program Help Using For Nested Loops

 
0
  #2
Oct 3rd, 2004
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:
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int a, b, c;
  8. int number;
  9.  
  10. cout<<"Enter a number from 0 - 999: ";
  11. cin>> number;
  12.  
  13. a = number / 100;
  14. b = (number % 100 - number % 10) / 10;
  15. c = number % 10;
  16.  
  17. for ( int i = 0; i <= a; i++ ) {
  18. for ( int j = 0; j <= ( ( i == a ) ? b : 9 ); j++ ) {
  19. for ( int k = 0; k <= ( ( j == b ) ? c : 9 ); k++ )
  20. cout<< i << j << k <<endl;
  21. }
  22. }
  23. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 29
Reputation: ZuK is an unknown quantity at this point 
Solved Threads: 0
ZuK ZuK is offline Offline
Light Poster

Re: Program Help Using For Nested Loops

 
0
  #3
Oct 3rd, 2004
is that what you want ?

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int number, c, d, e;
  8. cout << "Enter a Positive Integer from 0 to 999." << endl;
  9. cin >> number;
  10. while ((number < 0) || (number > 999))
  11. {
  12. cout << "Error: Negative Number or Number Greater than 999\n" << "Reenter that number and continue: \n";
  13. cin >> number; }
  14.  
  15. c = number / 100;
  16. d = (number % 100 - number % 10) / 10;
  17. e = number % 10;
  18.  
  19. cout << "c=" << c << "d=" << d << "e=" << e << endl;
  20. int k,i,j;
  21. for ( k= 0 ; k <= 9 ; k++)
  22.  
  23. for (i = 0; i <= 9 ; i++)
  24.  
  25. for ( j= 0; j <= 9 ; j++) {
  26.  
  27. cout << k << i << j << endl;
  28. if ( k == c && i == d && j == e )
  29. return 0;
  30. }
  31. return 1; // to keep the compiler happy
  32. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 32
Reputation: dontcare is an unknown quantity at this point 
Solved Threads: 0
dontcare dontcare is offline Offline
Light Poster

Re: Program Help Using For Nested Loops

 
0
  #4
Oct 3rd, 2004
Thanks narue and zuk.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: anemia is an unknown quantity at this point 
Solved Threads: 0
anemia anemia is offline Offline
Newbie Poster
 
-1
  #5
6 Days Ago
i want to get the programming format in c++ to display the output as:
*
**
***
using for loop
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC