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

Dani AI

Generated

is right: once you increment num1 to pass num2, your second loop’s condition is already false. Two simple fixes help here: (1) do not mutate the original inputs, and (2) compute clear bounds once, then print using separate counters (or a tiny helper).

Here is a compact approach that works for any order and avoids code duplication. It also keeps the user’s inputs intact and prints clean, space-separated lines.

#include <iostream>
#include <algorithm>  // std::min, std::max

void print_range(int a, int b) {
    int step = (a <= b) ? 1 : -1;
    for (int i = a; ; i += step) {
        std::cout << i;
        if (i == b) { std::cout << '\n'; break; }
        std::cout << ' ';
    }
}

int main() {
    int n1, n2;
    std::cout << "Enter two positive integers: ";
    if (!(std::cin >> n1 >> n2) || n1 < 0 || n2 < 0) {
        std::cerr << "Invalid input.\n";
        return 1;
    }

    int low = std::min(n1, n2);
    int high = std::max(n1, n2);

    print_range(low, high);   // low -> high
    print_range(high, low);   // high -> low
}

Notes and tie-ins:

  • If the numbers are equal, you will see the same single value printed twice (once per pass). If you only want it once, add a quick if (low != high) before the second call.
  • ’s swap explanation is spot on; if you prefer the standard library, std::swap(n1, n2) from <utility> does the same thing without a temp.
  • Using a dedicated counter (or the helper above) addresses the exact pitfall pointed out and makes the control flow obvious when you revisit the code later.

Recommended Answers

All 7 Replies

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.