I'm trying to write a program that will some all numbers that are multiples of 13, 15, and 17 but not 30. Anytime I run this code it gives me an output value of sum = 0. Can someone tell me what I'm doing wrong?
#include <iostream>
usingnamespace std;
int main( )
{
int upperbound;
cout << "This program will add together all numbers divisible by 13, 15, and 17 from 0 to a specified limit. Please specify a limit: ";
cin >> upperbound;
int sum = 0;
int number = 0;
while (number <= upperbound) {
if ((number % 13 == 0) && (number % 15 == 0) && (number % 17 == 0) && (number % 30 != 0))
sum += number;
else {
++number;}
}
cout << "The sum of all multiples of 13, 15, and 17 from 0 to " << upperbound << " is " << sum << endl;
main( );
return 0;
}
Just to clarify, you only want to add a number to the sum if that number is divisible by 13 and 15 and 17 but not 30 ? Meaning a number that's only divisible by, let's say, 17 would be left out, right ?
If so, looking quickly your code looks fine but I don't see the necessity of main(); on line 20.