The statement "sum+=i+j+k;" will be executed 4 * 5 * 5 = 100 times:
> for(int i=1;i<5;i++){ -->4
> *
> for(int j=10;j<15;j++){ -->5
> *
> for(int k=0;k<5;k++){ -->5
... i will be added 100 times, j will be added 100 times and k will be
added 100 times
i that changes from 1 through 4 will appear 25 times as 1, 25 times as
2, 25 times as 3 and 25 times as 4 contributing to the sum
1*25+2*25+3*25+4*25=250
j that changes from 10 through 14 will appear 20 times as 10, 20 times
as 11, 20 times as 12, 20 times as 13 and 20 times as 14 contributing
to the sum 10*20+11*20+12*20+13*20+14*20=1200
k that changes from 0 through 4 will appear 20 times as 0, 20 times as
1, 20 times as 2, 20 times as 3 and 20 times as 4 contributing to the
sum 0*20+1*20+2*20+3*20+4*20=200
250+1200+200=1650
On May 24, 5:41 am, SARAN S <[email protected]> wrote:
> Plz Could anyone explain the program, it prints sum=1650?????
>
> class forloop{
>
> public static void main(String args[])
> {
>
> int sum = 0;
>
> for(int i=1;i<5;i++){
>
> for(int j=10;j<15;j++){
>
> for(int k=0;k<5;k++){
>
> sum+=i+j+k;
>
> System.out.println("sum: "+sum);
>
> }//k
> }//j
> }//i
> }//main
>
> }//forloop class
>
> thanks
> regards
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---