+ 2

Cannot decide what the question requires.

I do not know if I have to find all multiples of the three numbers and then add them, or if I have to check in an if statement if a number is either the multiple of 5 or 7 or 9 (and if it happens to be for example 35, just add 35 once and not twice? Exercise : If we list all the natural numbers below 10 that are multiples of 5, 7or 9we get 5, 7, and 9. The sum of these multiples is 21. Find the sum of all the multiples of 5, 7, 9 below 5000.

1st Feb 2017, 6:02 PM
Maria Lazaridou
Maria Lazaridou - avatar
5 odpowiedzi
+ 9
for(int stp=1;stp<=5000;stp++){ if(stp%5==0||stp%7==0||stp%9==0){ sum+=stp; } } ----------------------------- (κατάλαβες;;) ----------------------------- public class Program { public static void main(String[] args) { int sum = 0; for(int stp=1;stp<=5000;stp++){ if(stp%5==0||stp%7==0||stp%9==0){ sum+=stp; } } System.out.print(sum); } }
1st Feb 2017, 6:12 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 8
πες τους να μαθουν τα logical gates πρωτα…… ^_^
1st Feb 2017, 6:22 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 8
@Maria I think you can only have 35 in there once. So from 1 to 35, the answer would be the sum of [5,7,9,10,14,15,18,20,21,25,27,28,30,35].
1st Feb 2017, 10:59 PM
Jafca
Jafca - avatar
+ 8
https://code.sololearn.com/cWe2T1vq5m8G/?ref=app ελπίδα αυτό βοηθά (Hope this helps) Sorry that it's in C# Leave a like if I was right Also, I included 5,000 in my answer. You might not have to...
1st Feb 2017, 11:16 PM
Jafca
Jafca - avatar
+ 1
That's exactly what I did but at the course I am taking they are now telling me that there should be three different if statements (one checking for each number) and then adding the three sums so as for example to include 35 twice in the sum? I do not agree. (αν δεν τους πείσω οτι έχω δίκιο θα σκάσω)
1st Feb 2017, 6:20 PM
Maria Lazaridou
Maria Lazaridou - avatar