Explaination Help (one more)

What is happening in this code?
why is the if(a==0 || b==0)in here and what is it doing?
what is happening in the for loop?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 #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;


}


Thank you!
Please read what is written in this statement

printf("\nThe sum of all the multiples of 3 or 5 below 1000: %d\n", iSum);
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
It looks like that somebody wrote the program for you made comments for the code but this did not help you to understand what the program does.:)

I would write the program the following way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <stdlib.h>

unsigned int return_sum()   //function to find the sum and return the sum
{
    unsigned int sum = 0;

    for ( unsigned int 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;
}
Thank you everyone for the help! Trying to get better at this ha
Topic archived. No new replies allowed.