Last active
August 16, 2017 14:16
-
-
Save RohithKumar1368/0bcb8a9bcf21b1a60e21cdf29472197d to your computer and use it in GitHub Desktop.
Project Euler 1 : To find the sum of all multiples of 3 or 5 below n.
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
#include<stdio.h> | |
int main(){ | |
int n ; | |
int sum ; | |
scanf("%d",&n) ; | |
int three = (n-1) / 3 ; // no of 3 multiples below n | |
int five = (n-1) / 5 ; // no of 5 multiples below n | |
int fifteen = (n-1) / 15 ; // no of 15 multiples below n | |
sum = 3 * (three * (three+1)) / 2 ; // finding sum of all multiples of 3 below n | |
sum += 5 * (five * (five+1)) / 2 ; // finding sum of all multiples of 3 below n | |
sum -= 15 * (fifteen * (fifteen+1)) / 2 ; // finding sum of all multiples of 3 below n | |
printf("%d\n",sum); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment