info@meritcampus.com    +91-85006-22255
...
`

Nested For loop output807
What will be the output of the following program?
`class NestedForloop{    public static void main(String s[])    {        int sum = 5;                int i = 1;        for( ; i <= 4; )        {            for(int j = 1; j <= 4; j++ )            {                sum += (i * j);                i++;            }        }        System.out.println("Sum = " + sum);    }}`

 A. Sum = 35 B. Sum = 51 C. Sum = 19 D. Compilation Error E. Runtime Error
Topic:

### User comments below.All of them might not be correct.

ans: A.here first for loop runs only one time and second for loop runs 4 times.              first iteration : j=1, i=1 ,sum=5+(1*!) i.e. 6.                2nd ite: j=2, i=2 nd sum=10.                                            3rd ite : j=3, i=3 nd sum=19.                        4th ite :  j=4 , i=4 nd sum=35.                           now j=5 condition false so exit inner for loop.check cond in outter for loop condition fails .prints sum=35;

Posted by Ananthoju Arun Chary    2014-11-17 03:55:24

Basic syntax of loop is for ( init; condition; increment )
{
statement(s);
}here in first loop only condition is mentioned,,so we have i=1,then control goes back to the second loop in which we have j=1,then sum=sum+i*j i.e is sum=5+1(i=1;j=1)
again increment condition is present i=i+1
similarly following conditions occur
i=1 j=1 sum=6
i=2 j=2 sum=10
i=3 j=3 sum=19
i=4 j=4 sum=35
as per condition i value can be less than equal to 4,,so when i=5 condition fails,,nd automatically prints the sum value as 35,,,
when there is no increment i++;
then sum=1+2+3+4=10
i=1 j=1 sum=6
i=1 j=2 sum=8
i=1 j=3 sum=11
i=1 j=4 sum=15
so increment value plays key role in nested loops,,,

Posted by Sai Veerendra    2014-11-17 04:59:49

ans is A,,, outer loop execute once time,, for second time i>5,,, inner for loop execute 4 time in each i and j same,,

Posted by Raju VarshnEy    2014-11-17 07:09:33

A

Posted by Vanaja Vannu    2014-11-17 16:41:19

ans a.                                                                                                                          in this program initially  int variables sum and i declared and initialized with 5 and 1.                                                                                                                             here nested loops used .                                                                                          outer for loop condition  true i<=4---1<=4.so inner for loop get executed.                 j=i;j<=4;j++  initially i=1 so j=1 j<=4 j++  sum+=(1*1)=6          aft i get incrmnted.                                                                                                                     i=2;j=2     2<=4;    j++                    thn sum+=(2*2)=10        i becom 3                              i=3,j=3         3<=4;j++              sum+=(3*3)=19                  i++==4.                                   i=4,j=4,4<=4            sum+=(4*4)=35         i++==5,                                                       i=5,j=5,    5<=4 condition fails control goes outoff inner for loop.                               thn outer for loop checks i with 4..i value is 5 so 5<=4 condition fails thn control goes outside of outer for loop.so outer for loop iterates only once.                  sop prints 35

Posted by Maheshwari Natarajan    2014-11-17 18:22:40

This dose is now closed and the winners are Sai Ram,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Uday Kumar. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-11-18 01:16:54

Ans is A) sum = 35

Posted by Vinodh Kumar    2014-11-18 05:11:04