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
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
+ 5
u did not declare sum and l as integers first after declaration u will get 65 as output
+ 3
It prints the sum of all the numbers from 1 to 11. It's simple...
+ 3
Ya @rajyalakshmi is correct
+ 3
first of sum=10 and sum + sum of first 10 integers =10+55= 65
+ 3
sum+=l gives sum=sum+l for every l values from 0 to 10
+ 3
welcome 😊 all d best for u r bright future
+ 2
thank you for the remarks.. how do you obtain 65?
+ 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
+ 1
thank you..
let's take another example maybe I will understand , sum+=I equal what?
+ 1
oooooh thaaaaaaank you!!
+ 1
for me sum+=I was sum=I+1.. thank you
+ 1
sum=10 11 12 13 ..20 ; ok.. what is the thing who give 65
+ 1
you beat me.. it was fun.. thank you.. I will learn the for loop again and I will duel with you :)
+ 1
look this code:
int i;
for(i=0;i<3;i+=2){}
System.out.print(i);
+ 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?
0
ooooh okaaaaay..