Won't calculate correctly

I am trying to have this output the number of multiples, not the numbers that are multiples. For example input of 7 and 23, I would want an output of 5 and 3, or the total number of multiples for each 3 and 5.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <iostream>

using namespace std;

int main() {
    // Write your main here
  int num1, num2;
  int i;
  int mod;
  int num;
    cout << "Enter two integers "<< endl;
    cin >> num1 >> num2;
  for (i=0; i<=num1;i++)
      if(i % 3 == 0 )
           cout << i << ' ';
  for (i=0; i<=num2; i++)
       if(i % 5 == 0)
           cout << i << ' ';
    return 0;
}
Last edited on
@ames1951

This is what you're probably looking for. Though you shouldn't start the checking at 0 in your for loops, as you'll get a wrong answer, as noted in your query. You can verify that its a bad answer by changing this code to start at 0 in lines 15 and 22, and inputting 2 and 4 for num1 and num2. You'll get an answer of 1 for each, and that is not correct, as there are NO multiples of either 3 or 5.
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
#include <iostream>

using namespace std;

int main() {
    // Write your main here
  int num1, num2;
  int ck;
  int i;
  int mod;
  int num;
    cout << "Enter two integers "<< endl;
    cin >> num1 >> num2;
	ck=0;
  for (i=1; i<=num1;i++)
  {
      if(i % 3 == 0 )
           ck++;
  }
  cout << "There are " << ck << " multiples of 3, in " << num1 << endl;
  ck = 0;
  for (i=1; i<=num2; i++)
  {
       if(i % 5 == 0)
           ck++;
  }
   cout << "There are " << ck << " multiples of 5, in " << num2 << endl;
   cin >> num1;
    return 0;
}
Last edited on
The instruction: The program outputs how many numbers are multiples of 3 and how many numbers are multiples of 5 between the two integers (inclusive).

I am having a problem figuring out how to get the number multiples of 3 and 5 for each number

Last edited on
@ames1951

The program outputs how many numbers are multiples of 3 and how many numbers are multiples of 5 between the two integers


It seems to me that what you wrote now, is that you need the number of mods between the inputted numbers, as in your example, 7 and 23.

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
#include <iostream>

using namespace std;

int main()
{
  int num1, num2;
  int ck,ck1;
  int i;
  
   cout << "Enter two integers "<< endl;
   cin >> num1 >> num2;
  ck=0;
  if(num1>num2) // Swap higher number and lower number
{
	ck=num2;
	num2=num1;
	num1=ck;
}
ck=0; // Initialize both to 0
ck1=0;

 for (i = num1; i <= num2; i++)
{
     if(i % 3 == 0 )
          ck++;
     if(i % 5 == 0)
          ck1++;
}
cout << "There are " << ck  << " multiples of 3, between " << num1 << " and " << num2 << endl;
cout << "There are " << ck1 << " multiples of 5, between " << num1 << " and " << num2 << endl;
cin >> num1; // Just to keep console open. 

return 0;
}

Ths is my next thought, but it only partially works

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
#include <iostream>

using namespace std;

int main() {
    // Write your main here
  int num1, num2;
  int ck;
  int i;
  int mod;
  int num;
    cout << "Enter two integers "<< endl;
    cin >> num1 >> num2;
	ck=0;
  for (i=1; i<=num2;i++)
  for (i=1; i<=num1; i++)	
  {
      if(i % 3 == 0  )
           ck++;
  }
  cout << "There are " << ck << " multiples of 3, in " << num1 << endl;
  ck =0;
  for (i=1; i<=num2; i++)
  	for (i=1; i <= num1;i++)
  {
       if(i % 5 == 0)
           ck++;
  }
   cout << "There are " << ck << " multiples of 5, in " << num2 << endl;
   cin >> num1;
    return 0;
}

Last edited on
@whitenite1, It can be done without a loop.

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

int count_mods(int first, int last, int m)
{
    if (first > last) std::swap(first, last);
    int first_included = (first % m == 0);
    first -= first % m;
    last -= last % m;
    return (last - first) / m + first_included;
}

int main()
{
    int first, last;
    std::cout << "Enter first and last number of range: ";
    std::cin >> first >> last;
    std::cout << "Divisible by 3: " << count_mods(first, last, 3) << '\n';
    std::cout << "Divisible by 5: " << count_mods(first, last, 5) << '\n';
}

Last edited on
If this is Project Euler problem 1, there is a formula to work this out. Also does one consider not counting numbers which are multiples of both the numbers, like 15, 30, 45 ... ?
Got it. Thanks!
Topic archived. No new replies allowed.