Write a program that prompts the user to enter two integers. The program outputs how many numbers are multiples of 3 and how many numbers are multiples of 5 between the two integers (inclusive).
#include <iostream>
int main()
{
std::cout << "Please enter two integers: ";
int first, second;
std::cin >> first >> second;
if(!std::cin || second < first)
{
std::cout << "Bad input\n";
return 1;
}
int counter3 = 0;
int counter5 = 0;
for(int i = first; i <= second; ++i)
{
if(i % 3 == 0)
{
++counter3;
}
if(i % 5 == 0)
{
++counter5;
}
}
std::cout << "The number of multiples of 3 is " << counter3 << "\n";
std::cout << "The number of multiples of 5 is " << counter5 << "\n";
return 0;
}
Comments
Leave a comment