Write a program that finds the sum of all numbers between 0 and 120 (inclusive) that are multiples of 4 or 5.BONUS: allow the user to input their own values for all four variables (both ends of the range and the factors to find multiples).
Apsara Oum-FiteAug 30, 2018My personal solution in Java: public static void main(String[] args) { int sum = 0; for(int i = 0; i <= 120; i++) { if(i % 4 == 0 || i % 5 == 0) { System.out.println(i); sum += i; } } System.out.println("Sum:\t" + sum);} BONUSpublic static void main(String[] args) { Scanner scan = new Scanner(System.in); int sum = 0, min, max, fact1, fact2; System.out.print("Enter the low end of the range:\t"); min = scan.nextInt(); System.out.print("Enter the high end of the range:\t"); max = scan.nextInt(); System.out.print("Enter one factor to find multiples of:\t"); fact1 = scan.nextInt(); System.out.print("Enter another factor to find multiples of:\t"); fact2 = scan.nextInt(); for(int i = min; i <= max; i++) { if(i % fact1 == 0 || i % fact2 == 0) { System.out.println(i); sum += i; } } System.out.println("Sum:\t" + sum);}
My personal solution in Java:
public static void main(String[] args) {
int sum = 0;
for(int i = 0; i <= 120; i++) {
if(i % 4 == 0 || i % 5 == 0) {
System.out.println(i);
sum += i;
}
}
System.out.println("Sum:\t" + sum);
}
BONUS
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int sum = 0, min, max, fact1, fact2;
System.out.print("Enter the low end of the range:\t"); min = scan.nextInt();
System.out.print("Enter the high end of the range:\t"); max = scan.nextInt();
System.out.print("Enter one factor to find multiples of:\t"); fact1 = scan.nextInt();
System.out.print("Enter another factor to find multiples of:\t"); fact2 = scan.nextInt();
for(int i = min; i <= max; i++) {
if(i % fact1 == 0 || i % fact2 == 0) {
System.out.println(i);
sum += i;
}
}
System.out.println("Sum:\t" + sum);
}