What is the output of this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

What is the output of this code?

int x=4; int y=6; sum=x+y; for (int I=1;I<=10;I++){ sum+=I ; } System.out.print(sum) explain me that.. it really bored me

29th May 2017, 6:00 AM
Issa Samake
Issa Samake - avatar
18 Answers
+ 4
for l=0 sum =sum+l =10+0=10, for l=1, sum=10+1=11,for l=2 ,sum=11+2=13 , for l=3,sum=13+3=16, for every iteration sum is updated with new value
29th May 2017, 6:35 AM
Rajyalakshmi Reddy
Rajyalakshmi Reddy - avatar
+ 5
u did not declare sum and l as integers first after declaration u will get 65 as output
29th May 2017, 6:05 AM
Rajyalakshmi Reddy
Rajyalakshmi Reddy - avatar
+ 3
It prints the sum of all the numbers from 1 to 11. It's simple...
29th May 2017, 6:04 AM
Vishnu ks
Vishnu ks - avatar
+ 3
Ya @rajyalakshmi is correct
29th May 2017, 6:05 AM
Vishnu ks
Vishnu ks - avatar
+ 3
first of sum=10 and sum + sum of first 10 integers =10+55= 65
29th May 2017, 6:10 AM
Rajyalakshmi Reddy
Rajyalakshmi Reddy - avatar
29th May 2017, 6:14 AM
Rajyalakshmi Reddy
Rajyalakshmi Reddy - avatar
+ 3
sum+=l gives sum=sum+l for every l values from 0 to 10
29th May 2017, 6:21 AM
Rajyalakshmi Reddy
Rajyalakshmi Reddy - avatar
+ 3
welcome 😊 all d best for u r bright future
29th May 2017, 6:22 AM
Rajyalakshmi Reddy
Rajyalakshmi Reddy - avatar
+ 2
thank you for the remarks.. how do you obtain 65?
29th May 2017, 6:06 AM
Issa Samake
Issa Samake - avatar
+ 2
if u closed the for loop after print statement u get output as 10,11 etc . for getting 65 for loop closed before print statement
29th May 2017, 6:31 AM
Rajyalakshmi Reddy
Rajyalakshmi Reddy - avatar
+ 1
thank you.. let's take another example maybe I will understand , sum+=I equal what?
29th May 2017, 6:19 AM
Issa Samake
Issa Samake - avatar
+ 1
oooooh thaaaaaaank you!!
29th May 2017, 6:21 AM
Issa Samake
Issa Samake - avatar
+ 1
for me sum+=I was sum=I+1.. thank you
29th May 2017, 6:23 AM
Issa Samake
Issa Samake - avatar
+ 1
sum=10 11 12 13 ..20 ; ok.. what is the thing who give 65
29th May 2017, 6:29 AM
Issa Samake
Issa Samake - avatar
+ 1
you beat me.. it was fun.. thank you.. I will learn the for loop again and I will duel with you :)
29th May 2017, 6:34 AM
Issa Samake
Issa Samake - avatar
+ 1
look this code: int i; for(i=0;i<3;i+=2){} System.out.print(i);
29th May 2017, 7:17 AM
Issa Samake
Issa Samake - avatar
+ 1
I=2+0=2; i=2 now, then I=2+2=4; I=4 now, then I=2+4=6; so I=6 finally?
29th May 2017, 7:20 AM
Issa Samake
Issa Samake - avatar
0
ooooh okaaaaay..
29th May 2017, 6:37 AM
Issa Samake
Issa Samake - avatar