0

Array

You are given a program that takes the length of the array as the first input, creates it, and then takes the next inputs as elements of the array. Complete the program to go through the array and calculate the sum of the numbers that are multiples of 4. Sample Input 5 4 9 16 2 7 Sample Output 20 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int length = scanner.nextInt(); int[] array = new int[length]; for (int i = 0; i < length; i++) { array[i] = scanner.nextInt(); } //your code goes here int sum = 0; for (int x = 0; x < array.length; x++){ if (array[x] % 4 == 0){ sum += array[x]; } System.out.println(sum); } } } https://sololearn.com/coach/539/?ref=app Why my code is not working. Plz help me!

20th Mar 2021, 12:29 AM
韩韔萌
韩韔萌 - avatar
7 Answers
+ 3
you are printing sum inside your last loop: you must move it outside... here's the corrected version of your code: https://code.sololearn.com/c39JHRhqq5pq/?ref=app
20th Mar 2021, 12:59 AM
visph
visph - avatar
+ 1
// one loop version, avoid array creation: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int length = scanner.nextInt(); int sum = 0; int val; for (int i=0; i<length; ++i) { val = scanner.nextInt(); if (val%4==0) sum += val; } System.out.println(sum); } }
20th Mar 2021, 1:12 AM
visph
visph - avatar
+ 1
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int length = scanner.nextInt(); int sum = 0; int val; for (int i=0; i<length; ++i) { val = scanner.nextInt(); if (val%4==0) sum += val; } System.out.println(sum); } } Good Luck
26th Jan 2022, 6:26 AM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
+ 1
Try this , int sum = 0; for (int x = 0; x < array.length; x++) { if (array[x] % 4 == 0) { sum += array[x]; } } System.out.println(sum); } }
14th Dec 2022, 6:48 PM
Mehmet Ali Aydin
+ 1
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int length = scanner.nextInt(); int[] array = new int[length]; for (int i = 0; i < length; i++) { array[i] = scanner.nextInt(); } //your code goes here int sum = 0; for (int i = 0; i < length; i++) { if (array[i]%4==0){ sum += array[i]; } } System.out.println(sum); } }
9th Apr 2023, 9:41 PM
Eden Mekonnen
Eden Mekonnen - avatar
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int length = scanner.nextInt(); int[] array = new int[length]; int sum = 0; //Please Subscribe to My Youtube Channel //Channel Name: Fazal Tuts4U for (int i = 0; i < length; i++) { array[i] = scanner.nextInt(); if(array[i]%4 == 0){ sum = sum + array[i]; } } System.out.println(sum); } }
3rd Sep 2021, 6:08 PM
Fazal Haroon
Fazal Haroon - avatar
0
can someone explain what the question is even asking? I don't understand what it wants me to do
13th Jul 2022, 2:11 PM
Affinity Gaming