#include <stdio.h>
#include <stdlib.h>
void return_sum () //function to find the sum and return the sum
{
int a,b,i,iSum=0;
for (i=1;i<1000;i++) //below 1000
{
a = i%3; //multiples for 3
b = i%5; //multiples for 5
if(a==0||b==0)
{
iSum = iSum+=i; //adding multiples of 3 and 5
}
}
printf("\nThe sum of all the multiples of 3 or 5 below 1000: %d\n", iSum); //print the sum
}
int main () {
return_sum(); //call to funciton return_sum
return 0;
}
The code is well documented and easy to read. If you can't understand what it is doing, look up the modulus(%) and equality(==) operators and all should become clear.
One silly thing wrong is that it does NOT return the sum but only prints it within the function despite the comment and the name of the function. It's a void function after all. 8^P
#include <stdio.h>
#include <stdlib.h>
unsignedint return_sum() //function to find the sum and return the sum
{
unsignedint sum = 0;
for ( unsignedint i = 1; i < 1000; i++ )
{
if ( i & 3 ==0 || i % 5 == 0 ) sum += i;
}
return sum;
}
int main( void )
{
printf( "The sum of all the multiples of 3 or 5 below 1000: %u\n", return_sum() );
return 0;
}