New to coding. Problem with simple code.

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?

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

using namespace 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;
}
Last edited on
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.
I want it to add the number to the sum if it's divisible by 13, 15, or 17 but not 30.

I know that this:
 
if ((number % 13 == 0) && (number % 15 == 0) && (number % 17 == 0) && (number % 30 != 0))

should be this:
 
if (((number % 13 == 0) || (number % 15 == 0) || (number % 17 == 0)) && (number % 30 != 0))

but if I do that, the program won't return anything after entering an upperbound.

Also, the point of main( ) is so that the cmd box doesn't close immediately after running the program.

Result with what is in original post: http://gyazo.com/7fa0707f6e44f5cb2ad58bc993b4a1e5

Result after I alter it to above: http://gyazo.com/f114f960220a6ca48901696730398d61
Last edited on
Topic archived. No new replies allowed.