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:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
#include <array>
usingnamespace 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?