Can we add and print the sum of 2 array in a single for loop? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can we add and print the sum of 2 array in a single for loop?

Arrays using for loop

20th May 2019, 7:22 AM
prem gupta
prem gupta - avatar
2 Answers
+ 4
Yes you can. You loop until the length of the longer array. And add the values of both arrays together at the same time. You just need to watch out to only access the array if the loop counter is still smaller than the array length. Because one array could be shorter than the other.
20th May 2019, 7:27 AM
Dragonxiv
Dragonxiv - avatar
+ 2
Yes. Aslong as the amount of subarrays is constant we can do that with one loop.
20th May 2019, 7:27 AM
Seb TheS
Seb TheS - avatar