for loop help

This is a discussion on for loop help within the C++ Programming forums, part of the General Programming Boards category; hi everyone im new to c++ programming. i need a little help with this for loop. say the user enters ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    3

    for loop help

    hi everyone im new to c++ programming.
    i need a little help with this for loop. say the user enters 5 this is supposed to be the output

    5
    54
    543
    5432
    54321

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int number;
    	cout << "Enter number" << endl;
    	cin >> number;
    	cout << endl;
    
    	for (int i = number; i > 0; --i)
    	{
    		for (int j = number; j > 0 ; j--)
    		{	
    			cout << j;
    		}
    
    		cout << endl;
    	}
    	return 0;
    }
    all i can get is 54321 .. 5 times... can someone help me out a little. thx in advance

  2. #2
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Working code:
    Code:
    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    
    int main()
    {
    	int number;
    	cout << "Enter number" << endl;
    	cin >> number;
    	cout << endl;
    	for(int b=0;b<=number;b++)
    	{
            for (int j = number; j>b ; j--)
    		{
    			cout << j;
    		}
    		cout<<endl;
        }
    	system("PAUSE");
    	return 0;
    }
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    3
    actually that is opposite
    that does this

    4321
    432
    43
    4

    i need

    4
    43
    432
    4321

    ivebeen tryin to reverse it but i cant get it
    Last edited by bkprgm; 10-05-2005 at 06:37 PM.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    jmd15's code prints it in reverse order.
    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
    	int number;
    	cout << "Enter number" << endl;
    	cin >> number;
    	cout << endl;
    
    	for (int i = number; i >= 0; --i) {
    		for (int j = number; j > i; j--)
    			cout << ".." << j;
    		cout << "\n";
    	}
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    3
    ahh thx rite when i was posting

  6. #6
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Yeah, same thing just reverse the order in which it's done. Instead of from big to small go from small to big.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21