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.
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.
#include <iostream>
usingnamespace 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;
}
#include <iostream>
usingnamespace 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;
}
#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';
}
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 ... ?