Monday, 24 October 2016

List all the natural numbers below 'N' that are multiples of 3 or 5.

/*
List all the natural numbers below 'N' that are multiples of 3 or 5.
sum_multiples(10)
we get, 3 + 5 + 6 + 9 = 23
*/

#include<stdio.h>
int sum_multiples(int n) {
 int i,sum=0;
 // Numbers from 3 to 'N'
 for(i=3; i<n; i++) {
  // Check for divisibility by 3 or 5
  if(i%3==0 || i%5==0) {
   // Add the number which is multiple of 3 or 5
   sum = sum + i;
  }
 }
 // return the result
 return sum;
}

int main() {
 int n;
 printf("Enter N:");
 scanf("%d", &n);
 printf("\nResult: %d", sum_multiples(n));
 return 0;
}

No comments: