Java Program to Sum Of Multiple of 3 and 5

Discussion DashboardCategory: JavaJava Program to Sum Of Multiple of 3 and 5
DHARMEDRA SAHU Staff asked 6 years ago
1 Answers
DHARMEDRA SAHU Staff answered 6 years ago

package come.dharma.projecteuler.net.problems;

public class SumOfMultipleOf3And5 {
public static void main(String[] args){
System.out.println(“Sum of Natural Number divisible by 3 and 5”);
System.out.println(“Sum is: “+sumOfMultipleOf3And5(100));
}
private static Long sumOfMultipleOf3And5(Integer num){
Long sum = new Long(“0”);
int counter = 0;
for(int i=3; i<num;){
if(i%3 == 0 ){
sum+=i;
}
i+=3;
counter++;
}
for(int i=5; i<num;){
if(i%5 == 0){
sum+=i;
}
i+=5;
counter++;
}
System.out.println("Total Loop Count :"+counter);
counter = 0;
sum = (long) 0;
for(int i=3; i<num; i++){
if(i%3 == 0 || i%5 == 0){
sum+=i;
}
counter++;
}
System.out.println("Total Loop Count Using Normal :"+counter);

return sum;
}

}