0

please help to solve this problem.

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"

4th Jun 2021, 8:22 AM
Somvardhan Shiva
Somvardhan Shiva - avatar
4 odpowiedzi
+ 10
You missed to declare sum variable at the beginning. You need to get the output outside loop int sum = 0; for (int i = 0;i < length; i++){ if (array [i]%4==0){ sum = sum + array[i]; } } System.out.println(sum);
4th Jun 2021, 8:35 AM
Simba
Simba - avatar
+ 1
Thanks Simba it worked.
4th Jun 2021, 8:42 AM
Somvardhan Shiva
Somvardhan Shiva - avatar
0
Get your spam outta here
4th Jun 2021, 8:28 AM
Slick
Slick - avatar
0
I written following code but where'r problems 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 for (int i = 0;i < length; i++){ if (array [i]%4==0){ sum = sum + array[i]; } } } }
4th Jun 2021, 8:29 AM
Somvardhan Shiva
Somvardhan Shiva - avatar