Modulo operation with all elements of an array

Hello,

I'm new to C++ and wanted to write some code which adds all natural numbers from 0 to the number given by the user except those, that are divisible by one of the numbers in an array. Here is my code:
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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
#include <array>
using namespace std;

int main()
{
	int x{ 1 };
	int sum{ 0 };
	int divisors[] = { 3, 5 };
	string userInput = "";

	cout << "Please enter a natural number: ";
	getline(cin, userInput);
	stringstream(userInput) >> x;

	for (int i = 0; i <= x; i++)
	{
		if (!(i % divisors[1] == 0))
			sum += i;
	}

	cout << "\nThe sum is: " << sum << "\n";

	return 0;
}


Right now it's just checking if it's divisible by 3, but can I make it do the modulo operation with all elements of the array without having to loop through it?

Thanks in advance!
Last edited on
You are testing divisibility by 5, for index 1 is the second element in an array. First element has index 0.

Yes, you need to loop somehow. For example:
http://www.cplusplus.com/reference/algorithm/none_of/
Topic archived. No new replies allowed.