Created
June 16, 2017 15:17
-
-
Save Bahaaib/583c31707caf5de620a38abd2f401e33 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /**Problem1:If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. | |
| The sum of these multiples is 23. | |
| Find the sum of all the multiples of 3 or 5 below 1000. | |
| */ | |
| /** | |
| * Created by Bebo on 6/16/2017. | |
| */ | |
| public class ex { | |
| public static void main(String[] args) { | |
| final int MAX = 1000; | |
| int threeMul = 0; | |
| int fiveMul = 0; | |
| int sum = 0; | |
| int natNums [] = new int[MAX]; | |
| for (int i=0; i<MAX; i++){ | |
| natNums[i] = i; | |
| } | |
| for (int j=0; j<MAX; j++){ | |
| if(natNums[j] % 3 == 0){ | |
| threeMul += natNums[j]; | |
| } | |
| if (natNums[j] % 5 == 0){ | |
| fiveMul += natNums[j]; | |
| } | |
| } | |
| sum = threeMul + fiveMul; | |
| System.out.println(threeMul); | |
| System.out.println(fiveMul); | |
| System.out.println(sum); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment