Skip to content

Instantly share code, notes, and snippets.

Created December 27, 2016 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/f31744f028ed93e64a97629bef2a98f2 to your computer and use it in GitHub Desktop.
Save anonymous/f31744f028ed93e64a97629bef2a98f2 to your computer and use it in GitHub Desktop.
Find the sum of all the multiples of 3 or 5 below 1000.
/*
Copyright 2016 strupo
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
/* This program sums the multiples of 3 and 5 less than 1000.
*/
int main (void)
{
int i; /* counter */
int start = 0; /* start summing from here */
int stop = 1e3; /* stop summing when you reach this */
int mult[2]; /* this array contains the numbers whose multiples will be summed */
long sum = 0L; /* will contain the sum; initialized to zero */
mult[0] = 3;
mult[1] = 5;
for (i = start; i < end; ++i) {
if (!(i % mult[0] && i % mult[1])) { /* :DDD */
sum += (long) i;
}
}
printf("Sum of multiples of %d and %d from %d to %d = %ld\n",
mult[0], mult[1], start, end, sum);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment