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

Calculate sum of numbers using while and break
What will be the output of the following program?
`class WhileExample{    public static void main(String s[])    {        int n = 0;        int sum = 0;        int count = 10;        while( n <= 3 )        {            sum += (n * 2);            if( count < 7 )            {                break;            }            count--;            n++;            if( n % 2 == 0 )            {                sum = sum * 3;            }        }        System.out.println("sum = " + sum);    }}`

 A. sum = 12 B. sum = 48 C. sum = 0 D. sum = 14
Topic:

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

Ans is B as the additions and multiplications related to sum variable with incrementing and decrementing of n and count will continue upto count is less than 7

Posted by Sai Avinash    2014-08-21 12:45:32

B. while loop                  if loop
1.n=0 ,so sum =0     -->    1.n=1,sum =0
2.n=1,sum =2         -->    2.n=2,sum =6
3.n=2,sum=10         -->    3.n=3,sum =10
4.n=3,sum =16        -->    4.n=4,sum=16*3=48

Posted by Prabhakar Reddy    2014-08-21 12:58:44

Congratulations Sai Avinash. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-08-22 05:13:47