Complete the program to go through the array and calculate the sum of the numbers that are multiples of 4. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Complete the program to go through the array and calculate the sum of the numbers that are multiples of 4.

Can anyone help me with this program?

3rd Mar 2021, 3:02 PM
Suraj Datta
Suraj Datta - avatar
5 Answers
+ 2
Thnx mate for the solution :)
3rd Mar 2021, 3:39 PM
Suraj Datta
Suraj Datta - 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[] array = new int[length]; for (int i = 0; i < length; i++) { array[i] = scanner.nextInt(); } //your code goes here double sum = 0.0; if(array[i] % 4 == 0){ sum += array[i]; } System.out.println(sum); } }
3rd Mar 2021, 3:14 PM
Suraj Datta
Suraj Datta - avatar
0
Yes , can you show us your attempt please ? Also use relevant language name as tags .
3rd Mar 2021, 3:05 PM
Abhay
Abhay - avatar
0
Please, show us your attempt first. Or you do not understand the task?
3rd Mar 2021, 3:06 PM
#0009e7 [get]
#0009e7 [get] - avatar
0
You should iterate through array and the "if" statement should be checked at each iteration. Fixed version: https://code.sololearn.com/cv53Eec7J1PE/?ref=app
3rd Mar 2021, 3:21 PM
#0009e7 [get]
#0009e7 [get] - avatar