Find the sum of all the multiples of 3 or 5 below 1000.
Hi all I have written the following code in C language to 'Find the sum of all the multiples of 3 or 5 below 1000' it works fine for i<10 and i<100 and also gives the right answer but for condition i<1000 the answer is in -ve something like -28345. I am trying to understand waht is wrong and how to correct it. (I have a clue of what is wrong :-) but want to hear from the experts here)
Code:
//Find the sum of all the multiples of 3 or 5 below 1000.
#include<stdio.h>
#include<conio.h>
void main ()
{
int a,b,i,iSum=0;
for (i=1;i<1000;i++)
{
a = i%3;
b = i%5;
if(a==0||b==0)
{
iSum = iSum+i;
}
}
printf("\nThe sum of all the multiples of 3 or 5 below 1000: %d\n", iSum);
getch();
clrscr();
}