Page 1 of 1

My Java program for project Euler problem 1

Posted: Fri Feb 24, 2012 12:33 pm
by Nipuna
Hi

I recently joined a cool website, I think as programmers all you guys know about this site before me.

http://projecteuler.net

For people who don't know I will explain a bit, This site is for people who likes to improve their programing knowledge or for learners to gain their knowledge. There are more than 300+ problems to solve.

Although I joined this site about 3 weeks ago but only made the working program for the problem 1 today and submitted. :)

I made programs before for this problem but they didn't work. I used AND operator before and it didn't work. So I made the program today with Else and Else If statements And it worked.
But the shortest way to solve this is using OR operator. But I forgot about it. :)
Anyway, I am happy because my program works. :)

So enough talking, Here is the code :)

Code: Select all

class Solution_for_Question1{
	public static void main(String args[]){																	
		int total=0;
        
		for(int counter = 1; counter<1000; counter++){
			if(counter%3==0){
				System.out.println("OK with 3 "+counter);
				total+=counter; // Exactly same as total=total+counter;
			}
			else if(counter%5==0){
				System.out.println("OK with 5 "+counter);
				total=total+counter; // Exactly same as total+=counter;
			}
		}
		System.out.println();
		System.out.println("Total is: "+total);
	}
}