MHB Using a while loop to print the counting numbers up to a set value

AI Thread Summary
The discussion focuses on correcting a while loop in C++ that is intended to print numbers from 1 to a given positive integer, userNum. The initial code incorrectly counts down from userNum instead of counting up. To fix this, the loop should use the variable i as an index, starting from 1 and incrementing until it exceeds userNum. The correct implementation involves initializing i to 1, using a while loop that checks if i is less than or equal to userNum, and printing i followed by a space. This adjustment ensures the output matches the expected format, displaying numbers in ascending order.
needOfHelpCMath
Messages
70
Reaction score
0
Write a while loop that prints 1 to userNum, using the variable i. Follow each number (even the last one) by a space. Assume userNum is positive. Ex: userNum = 4 prints:
1 2 3 4

Code:
#include <iostream>
using namespace std;

int main() {
   int userNum = 0;
   int i = 0;

   userNum = 4;    // Assume positive
   
   
while ( userNum > i) {
cout << userNum << " ";
userNum = userNum - 1;
}

   cout << endl;

   return 0;
}
Run
✖ Testing with userNum = 4
Expected output: 1 2 3 4
Your output: 4 3 2 1
 
Technology news on Phys.org
Re: What i am doing wrong?

You need to use [m]i[/m] to "index" your while loop, which should run as it is less than or equal to [m]userNum[/m]. Since [m]i[/m] is initialized to zero, you will want to increment it before the comparison, using [m]++i[/m], and then you will not need to increment it within the loop, you will only need to print its value along with the trailing space.
 
Re: What i am doing wrong?

MarkFL said:
You need to use [m]i[/m] to "index" your while loop, which should run as it is less than or equal to [m]userNum[/m]. Since [m]i[/m] is initialized to zero, you will want to increment it before the comparison, using [m]++i[/m], and then you will not need to increment it within the loop, you will only need to print its value along with the trailing space.

"Awesome To The Max"
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
1
Views
6K
Replies
2
Views
13K
Replies
4
Views
11K
Replies
28
Views
30K
Replies
1
Views
1K
Replies
1
Views
2K
Back
Top