I can't find the logic or it's just not possible?

Hello guys, this is my second post. I have a question about While-Loop with only ONE variable.

Look at this code and run it if you want:

#include <iostream>
#include <conio.h>
using namespace std;

int main ()
{
int n;
cout << "Enter the number: ";
cin >> n;
cout << endl;
while (n>=0)
{
cout << n << " " << endl;
--n;
}
cout << endl;
cout << "Count down is done.";
getch();
}


Now, can you do the code of the output being: 1 2 3 4 5 6 7 8 9 10.

It's in ascending order.. counting up..

Is it possible to also use ONLY one variable again with incrementing using " n++ " on that WHILE loop. I tried many times but the result is always infinite loop.
Last edited on
Are you trying to make it where you type a number and it will count up to 10 then stop?

So if I type "4"... it will go 4 5 6 7 8 9 10?

Edit:

Is this what you want:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <conio.h>
#include <Windows.h>
using namespace std;

int main()
{
	int n;
	cout << "Enter the number: ";
	cin >> n;
	cout << endl;
	while (n <= 10)
	{
		cout << n << " " << endl;
		n++;
	}
	cout << endl;
	cout << "Count down is done.";
	Sleep(10000);

}
Last edited on
No. For example, you will enter a number "10" then it will count start from 1 to 10. If you enter 100, it will count which starts from 1 to 100th.

Can you possibly use only one variable and increment in a simple "WHILE" loop?


-Use a simple WHILE loop like the one above.
-Use only one variable "n".

For example:

Output would be:

Enter the number: 5

1
2
3
4
5



I'm trying to figure out how to get that output with one variable and incrementing n++, which fails because it will loop infinitely. Any ideas?
Last edited on
This does what you want but it uses int i and int n. If it has to be 1 variable I will keep trying to figure a way for ya.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <conio.h>
#include <Windows.h>
using namespace std;



int main()
{
	int n;
	cout << "Enter the number: ";
	cin >> n;
	cout << endl;
	for (int i = 1; i < n; i++)
	{
		cout << i+1 << " " << endl;
	}
	cout << endl;
	cout << "Count down is done.";
	Sleep(10000);
}


Just remembered you have to do this with a while loop lol. I really don't know what to do.
Last edited on
This does what you want using a while loop. However, I couldn't think of any way to do it using only one variable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main()
{
    int n = 0, i = 1;

    cout << "Enter a number: " << endl;
    cin >> n;

    while(i < (n + 1))
    {
        cout << i << " ";
        i++;
    }

    return 0;
}
Last edited on
You could use an array with two values. Still technically only one variable though.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main()
{
	int n[2];
	n[1] = 0;

	cout << "Enter a number: " << endl;
	cin >> n[0];

	while (n[1] < (n[0] + 1))
	{
		cout << n[1] << " ";
		n[1]++;
	}
	return 0;
}

Same code as Bogeyman, but with an array.
It's sort of cheating but what about doing it recursively?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

void countUp(int n)
{
    if (n <= 0) {
	return;
    } else {
	countUp(n-1);
	cout << n << '\n';
    }
}
int main()
{
    int n;
    cout << "Enter the number: ";
    cin >> n;
    countUp(n);
    return 0;
}

Why "just one variable"?
closed account (D80DSL3A)
Rules?
1) Use one variable only.
2) Use a while loop.
3) Must use ++n.

Any others not yet specified?

Which rule does the following approach break?

Squeeze more from the single variable.
Let it hold both the count limit (in the high bits) and the increasing count (in the low bits).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>

int main()
{
    int n;
    std::cout << "Enter the number: ";
    std::cin >> n;

    n *= 10000;// bury the count limit in the upper digits
    ++n;

    while( n%10000 <= n/10000 )
    {
        std::cout << n%10000 << " " << '\n';
	n++;
    }

    std::cout << '\n';
    return 0;
}

EDIT: Removed const int c = 10000, which would have been a 2nd variable!
Actually, would that count since it's constant and therefore not a "variable"?
Last edited on
Topic archived. No new replies allowed.