Friday 8 August 2014

Problem 1

Problem 1

First problem from project Euler.
I thought when I had some free time and nothing to write about I would see how many of these I could get though. I'm not going to write the actual answer down, but I'll just show the code which gave me the correct answer. Feedback on better ways to do these are most welcome!

Question:

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.


Answer:

public class problem1 {

public static void main(String[] args){
    int sum = 0;
    for(int i = 1; i < 1000; i++){
            if(i % 3 == 0 || i % 5 == 0){
               sum = sum + i;
            }
       }
   System.out.println(sum);
}


No comments:

Post a Comment