Euler Project 1 Solution
Ahh, yes. Good ol’ Project Euler. Here’s my solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import java.util.Scanner; //Program to solve Euler Project One //kuemerle5 //Block 2 public class euler1{ private int inputNum; private int factorOne; private int factorTwo; private int numSum; //This method simply gets the user's input public void getInfo(){ Scanner in = new Scanner(System.in); System.out.print("Please enter the number you wish me to find the sum of two numbers: "); inputNum = in.nextInt(); System.out.print("Please enter the first multiple: "); factorOne = in.nextInt(); System.out.print("Please enter the second multiple: "); factorTwo = in.nextInt(); System.out.println("Thank you for your input. We will now do some calculations. Stand by..."); } //This is the main part of the program, using a "for" statement, then a nested "if" statement public void doWork(){ for(int runAmt=1; runAmt<inputNum; runAmt+=1){ if(runAmt%factorOne==0 || runAmt%factorTwo==0){ numSum += runAmt; } } } //The "getOutput" method prints out the results of the "doWork" method public void getOutput(){ System.out.println("Sum is: " + numSum); } } |
