Friday, November 20, 2009

Name Java. easy


code:

public class Name {

public static void main(String [] args) {
System.out.println(Name.line());
}

public static String line() {
String name = "This is my name";
return name;
}

}

Thursday, November 19, 2009

ICSP - Euler Project 1 in Java

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:

//Written by Shane Conroy
public class ProjectEuler1
//Ok, so first we create 3 Variables
//MaxInteger states the number you are getting multiples up to
//numberOne states the first number you are getting multiples of
//numberTwo states the second number you are getting multiples of
//the word FINAL is used so that the variables cannot be changed

public static final int maxInteger=1000;
public static final int numberOne=3;
public static final int numberTwo=5;

//void is here because the 'main' line does not return any values
public static void main(String[] args) {
System.out.println("Number of multiples of "+numberOne+" and "+numberTwo+" below "+maxInteger+" is: "+ProjectEuler1.solve());
}

public static int solve() {
int total=0;
for (int i=1; i
if (i%numberOne==0||i%numberTwo==0) {
// Do two statements parallel. Makes sure 15 is not repeated because 3*5=15.
// Gets modulus (remainder) of each and if they have no remainder then add number to total
total= total+i;
}
}

return total;
}
}