School Question - Looping

So learning looping. The question is "Write a program using a do while loop that shows output from 1 to 10 going up in ones but excludes 6 and 8

Hint: nest an if statement inside the loop"

Unsure how to exclude numbers from the loop, so far I have.. unsure what to put into my else if.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  int main(void)
{

	int number = 1;

	do
	{
		if (number <= 10)
		{
			cout << "number is " << number << "\n";
			number = number + 1;
		}
		else if (number == 6 || number == 8)
		{

		}
	
	} while (number <= 10);

Why do you think you need to put anything in the "else if"? What is it that you think your program should be doing when number is 6 or 8?

EDIT: In any case, your logic is messed up. if number is 6 (or 8), then the condition number <= 10 is true, so the number will be output anyway. You need to rethink the logic.
Last edited on
If the hint is not part of the actual requirement, this is also valid.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <cmath>

int main()
{
    int number = 1;
    while (number <= 8)
    {
        std::cout << (int)(1.505 * number + 1.99 * std::exp(-1.331*(number-1)) - 1.535) << '\n';
        number++;
    }
}


http://www.wolframalpha.com/input/?i=plot+y+%3D+n,+y+%3D+1.505+*+n+%2B+1.99+*+exp(-1.331*(n-1))+-+1.535+from+n+%3D+1+to+10
Last edited on
Topic archived. No new replies allowed.