Need help with my code

So my code works, but I want to break out of my loop when the counter is a multiple of 2, 3, and 5, which is when it equals to 30. However when I compile it I want to display to the screen that 30 is a multiple of 2 3 and 5. Also the way I used the break seemed kind of a cheap way to do it, is there another way I can break the loop when it is the 3 multiples? Thanks

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

using namespace std;

int main()
{
	int counter = 0;

	while (1)
	{
		counter += 1;
		if (counter > 30)
		break;
			cout << "current counter value:" << counter << "\n";
			
		
	}
}
Use the modulo(%) operator.
1
2
3
4
5
do
{
	counter += 1;
	std::cout << "current counter value:" << counter << "\n";
} while (counter % 2 || counter % 3 || counter % 5);

The loop will run until the counter variable is divided into by all three numbers without a remainder.
When I get that its right but when it gets to 30 I want it do display that its a multiple of 2 3 and 5 so I want it to be like this

current counter value: 1
.
.
.
current counter value: 29
current counter value: 30 is a multiple of 2, 3, and 5

hope that makes sense thanks.
Move line 4 outside the loop.
I tried that and it only displayed

current counter value: 30

I need it so it displays from 1 all the way to 30
heres my code

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

using namespace std;

int main()
{
	int counter = 0;

	do
	{
		counter += 1;
		cout << "current counter value " << counter << "\n";
	} 
	while (counter % 2 || counter % 3 || counter % 5);

	cout << "current counter value: " << counter << " is a multiple of 2, 3, and 5\n";

	cin.get();
	return 0;
}


But when it shows me it shows 1 through 30 but it shows 30 twice
"current counter value: 30
current counter value: 30 is a multiple of 2, 3, and 5"

I only want it to show the second one while also including the counter values of 1-29.
"current counter value: 30
current counter value: 30 is a multiple of 2, 3, and 5"

I only want it to show the second one while also including the counter values of 1-29.


Then just do this:
 
cout << " is a multiple of 2, 3, and 5\n";
ok thank you
Topic archived. No new replies allowed.