+ 1

How to Add two Arrays of 5 integers and print the sum of the two using Java ?

24th Sep 2020, 5:49 AM
Reynaldz Parse
Reynaldz Parse - avatar
6 Réponses
+ 1
Add this to your code: for(int i = 0; i < arr1.length; i++){ System.out.print(arr1[i] + arr2[i]); if(i != arr1.length - 1){ System.out.print(", "); } }
24th Sep 2020, 12:01 PM
Zohreh Karimi
Zohreh Karimi - avatar
+ 2
Thank You and Congrats to you Z.K for the correct answer. You've got it!
24th Sep 2020, 12:36 PM
Reynaldz Parse
Reynaldz Parse - avatar
+ 1
For example: int arr1[ ] = { 1,2,4,5,6 }; int arr2[ ] = { 2,3,5,6,2 }; Output: 3, 5, 9, 11, 8 Question : How to Add Two Arrays of 5 integers and print the sum of the two arrays using Java? Let me share and show here your own answer guys. Who can answer? Let's doing it in here. Thank you!
24th Sep 2020, 6:08 AM
Reynaldz Parse
Reynaldz Parse - avatar
+ 1
public class AddingTwoArraysIntegers { public static void main(String[] args) { int arr1[] = {1,2,3,4,5,6,7,8,9,10}; int arr2[] = {2,3,4,5,6,7,8,9,2,3}; for (int i=0; i<arr1.length; i++){ if (i!=arr1.length){ System.out.println("The sum of two arrays integers are:"); System.out.println(arr1[i] + arr2[i]); } } } }
24th Sep 2020, 1:00 PM
Reynaldz Parse
Reynaldz Parse - avatar
0
please show attempt. you already know arrays and iterating them. It is 80% of this.
24th Sep 2020, 5:56 AM
Oma Falk
Oma Falk - avatar
0
for loop from 0 to 4 and add arr1[i] and arr2[i].
24th Sep 2020, 6:11 AM
Oma Falk
Oma Falk - avatar