Wednesday, 5 August 2015

Find the sum of all multiples of 3 and/or 5 that are less than a given N, Code Fight Problem Solution.

/*
Find the sum of all multiples of 3 and/or 5 that are less than a given N.
Example
N = 10 -> 3, 5, 6, 9 -> 3 + 5 + 6 + 9 = 23


Input 1 (N) → integer :

N < 1000000
Output → integer :

the required sum



TAG: Easy Solution, Algorithm,CodeFight, Math Problem, Summation Problem.
*/

Solution:

#include <iostream>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int Sum_Of_Multiples_3_Or_5(int N) {
  int sum = 0;
  for(int i = 3; i < N; i++){
   if(i%3 == 0 || i%5 ==0)sum+=i;
  }
  return sum;
}
int main() {
   int n;
   cin >> n;
   cout << Sum_Of_Multiples_3_Or_5(n) << endl;
   return 0;
}

No comments:

Post a Comment