Hello, basically I'm trying to write a program where you ask the user to input 2 positive number. Then the program will print the lower of the two numbers to the higher of the 2 numbers and then higher of the 2 numbers to the lower of the 2 numbers.

I got it to print the numbers from low to high but I can't get it to print from high to low(reverse order)

example - user inputs 2 and 8
output - 2 3 4 5 6 7 8
8 7 6 5 4 3 2(reverse order)

#include <iostream>

using namespace std;

int main()
{

int num1 = -1;
int num2 = -1;

while(num1 < 0)
{
    cout << "please enter a positive integer: "; //asks the user to input number
    cin >> num1;
}
while (num2 < 0)
{
    cout<<"Please enter your second positive number: "; // asks the user to input second number
    cin >> num2;
}

while(num1<=num2)  //prints number from low to high
{

    cout<<num1;
    num1++;

}
while(num1>=num2) //prints number from low to high
{
    cout<<num2;
    num2++;

}


    return 0;
}

When you print low to high using loop, you incremented num1 and therefore the second loop never runs.

When you print low to high using loop, you incremented num1 and therefore the second loop never runs.

ooh
what do you suggest I use then?

I would use two for() loops. You can use a while() loop but I tend to avoid them when you want to count through a range of values.

#include <iostream>
using namespace std;

int main()
{
	int num1, num2;

	cout << "Enter a positive integer: " << endl;
	cin >> num1;
	cout << "Enter another positive integer: " << endl;
	cin >> num2;

	if( num1 > num2 )
	{
		int tmp = num1;
		num1 = num2;
		num2 = tmp;
	}

	//low to high
	for( int i = num1; i <= num2; i++ )
		cout << i << " ";
	cout << endl;

	//high to low
	for( int i = num2; i >= num1; i-- )
		cout << i << " ";
	cout << endl;

	return 0;
}

I didn't throw in any checking for the two inputs.

I would use two for() loops. You can use a while() loop but I tend to avoid them when you want to count through a range of values.

#include <iostream>
using namespace std;

int main()
{
	int num1, num2;

	cout << "Enter a positive integer: " << endl;
	cin >> num1;
	cout << "Enter another positive integer: " << endl;
	cin >> num2;

	if( num1 > num2 )
	{
		int tmp = num1;
		num1 = num2;
		num2 = tmp;
	}

	//low to high
	for( int i = num1; i <= num2; i++ )
		cout << i << " ";
	cout << endl;

	//high to low
	for( int i = num2; i >= num1; i-- )
		cout << i << " ";
	cout << endl;

	return 0;
}

I didn't throw in any checking for the two inputs.

this community is really helpful!!
The program you showed didn't work when num1 was bigger than num2 but i got around that!

thanks guys

I don't see how it doesn't work if you input num1 larger than num2.

sfuo can u explain this code please.

if( num1 > num2 )
{
int tmp = num1;
num1 = num2;
num2 = tmp;
}

You have to create a temporary holding variable since you cannot just write

num1 = num2;
num2 = num1;

since this will result in them both being equal to num1.

With the temporary variable there you store the original value of num1 then you change the value of num1 but you still hold its previous value in tmp. Then you can make num2 equal to tmp. All this does is swaps the two numbers.
This has to happen because below we assume that num1 is greater than num2, and if this isn't true then it will not work as intended (it will loop forever).

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.