-
-
Save UmarIqbal/6159612 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void Main(string[] args) | |
{ | |
long result=0; | |
result+=3*multtiple(999, 3); // it will first sum of multiples of 3 then multiply it by 3 | |
result+=5*multtiple(999, 5); // same for 5 | |
result-=15*multtiple(999, 15); // to remove repeting values(multiples of both 3 and 5) | |
Console.WriteLine("Sum of multiples of 3 and 5 :" +result ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static long multiple(int range, int num) | |
{ | |
long n=0; | |
for (int i = range; i > 0; i--) //it will give us largest number within the range that is | |
if (i % num == 0) //either the multiple of 3 or 5 | |
{ | |
n = i/num; | |
break; | |
} | |
return (n*(n+1))/2; //It will five sumation of series till the range(explained below in series) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment