0

Guys help me with this question , my code isnt working According to the specifications

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 //My code 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[] array = new int[length]; for (int i = 0; i < length; i++) { array[i] = scanner.nextInt(); } for(int x=0; x < array.length; x++) { if(x %4==0){ sum+=array[x]; } } System.out.println(sum); } }

2nd Feb 2021, 1:51 AM
BRIGHTON BANDA
BRIGHTON  BANDA - avatar
5 odpowiedzi
+ 3
Check your if condition. You need to check if the element at index x is a multiple of 4. You're currently checking if x itself is a multiple of 4.
2nd Feb 2021, 2:04 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
ChaoticDawg too lit you are,,, thanks
2nd Feb 2021, 2:09 AM
BRIGHTON BANDA
BRIGHTON  BANDA - avatar
0
if (array[x] % 4 == 0)
8th Apr 2021, 2:30 PM
Dima Kondratenko
Dima Kondratenko - avatar
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int sum=0; int length = scanner.nextInt(); int[] array = new int[length]; for (int i = 0; i < length; i++) { array[i] = scanner.nextInt(); } //your code goes here for(int x=0; x < array.length; x++) { if(array[x] %4==0){ sum +=array[x]; } } System.out.println(sum); } }
29th Jun 2021, 8:50 AM
Gowri Kumar
Gowri Kumar - avatar
0
Instead of x in the if condition, Use array[x]
17th Oct 2022, 1:21 PM
Jaya Sai Srikar
Jaya Sai Srikar - avatar