I have a question in java.How and why we find the following output 20 of y? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I have a question in java.How and why we find the following output 20 of y?

int arr[]={1,2,3,4,5}; int x=4; int y=0; for(int i=0;i<5;i++) y+=arr[x-i]*i; System.out.println(y);

12th Aug 2020, 6:31 AM
Ebney Samrat
2 Answers
+ 2
When i = 0: y += 0; Because arr[4 - 0]*0 ---> 0. When i = 1: y += 4; Because arr[4 - 1]*1 ---> 4*1 ---> 4. When i = 2: y += 6; Because arr[4 - 2]*2 ---> 3*2 ---> 6. When i = 3: y += 6; Because arr[4 - 3]*3 ---> 2*3 ---> 6. When i = 4: y += 4; Because arr[4 - 4]*4 ---> 1*4 ---> 4. 0 + 4 + 6 + 6 + 4 ---> 20.
12th Aug 2020, 6:44 AM
Seb TheS
Seb TheS - avatar
0
5*0+4*1+3*2+2*3+1*4=20
12th Aug 2020, 7:20 AM
The future is now thanks to science
The future is now thanks to science - avatar