Algorithm to determine multiples? W/o need for libs or extra includes.

closed account (G30oGNh0)
Consider the following code.

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

using std::cin;
using std::cout;

int main()
{
	for(int i = 0; i <= 100000; i++)
	{
		cout << "\r" << i;

		if(i == 10000 || i == 20000 || i == 30000 || i == 40000) // etc, Multiples of ten thousand
		{
			cout << "\a";
		}
	}
	cin.get();
	return 0;
}


Want to change the OR's in the if statement for something a little more elegant.

Any mathamagicians up for the challenge? No Libraries/Extra Includes can be used! Can it even be done? Looking forward to the responses.
closed account (o3hC5Di1)
Hi there,

A nifty little trick for this kind of thing is to use the modulus operator %.
It returns the remainder of the division between its left operand and its right operand.

Few examples:

2 % 2 = 0
3 % 2 = 1
6 % 4 = 2
20000 % 10000 = 0

So in your code you could test for this using:

if ( (i%10000) == 0 )

Hope that helps.

All the best,
NwN
closed account (G30oGNh0)
Great answer, I was expecting the Modulus to come into play first as it's the most easiest. Great non the less.

Any other takers?

EDIT: Will mark this as solved at 20:30.
Last edited on
Topic archived. No new replies allowed.