Number of two multiples between two integers

Hello. I'm trying to get a program to display the number of multiples of 3 and 5 between two integer inputs. Like if you enter 100 and 1, it would give 33 and 20 respectively. I came up with the basic parts of the program but I'm stumped as to how to do the actual main part.

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

using namespace std;

int main() 
{
    int num1;
    int num2;
    int sum3 = 0;
    int sum5 = 0;
    
    cout << "Enter two integers to find the multiples of 3 and 5 between them " << endl;
    cin >> num1 >> num2;
    
       while ()
           ++sum3;
       while ()
           ++sum5;
    
    cout << "Multiples of 3 = " << sum3 <<endl;
    cout << "Multiples of 5 = " << sum5 <<endl;
    return 0;
}


I'm assuming there are "while" statements used but I'm not sure how to continue with them.
Any help would be very appreciated.
Subtract the two numbers (doesn't matter which way) and put the value into abs (or check if it is negative and swap the sign if so). Then divide it by either 3 or 5. You should use floating point values, primarily the type double because 33*3 = 99 not 100 (you want 33.3~), but at the same time the distance between 100 and 1 is 99 not 100 (aka: 100-1=99). I think what you want is 0 to 100.
It's really not that hard, all you need is to check for all numbers between your numbers, if they are multiples of 3 and 5. like so if (yourNumber % 5 == 0) {/*your logic*/}

As far as loops go, I don't think a while loop is necessary, you can simply use a for loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>

using namespace std;

int main() 
{
    int begin;
    int end;
    int sum3 = 0;
    int sum5 = 0;
    
    cout << "Enter two integers to find the multiples of 3 and 5 between them\n"
    	<< "Number 1: ";
    cin >> begin;
    cout << "Number 2: ";
    cin >> end;

    if ( begin > end ) //Just in case the first number you input is larger than the second
    {
	int temp = begin;
	begin = end;
	end = temp;
    }
	
    for (int count = begin; count <= end; count++)
    {
	if ( count % 5 == 0 )     // if the rest from the division count/5 is equal to 0
		sum5++;
			
	if ( count % 3 == 0 )     // if the rest from the division count/3 is equal to 0
		sum3++;
    }
	
    cout << "Multiples of 3 = " << sum3 << "\n"
        <<  "Multiples of 5 = " << sum5 << "\n";
    
    system ("PAUSE");
    
    return 0;
}


Hope this helps
Last edited on
Hi,

This sounds a lot like Euler Question 1.

If so, a clue is that there is a formula to work this out directly, no brute force necessary.

And make sure not to count multiples twice, like 15 for example.
Topic archived. No new replies allowed.