Menu
Topics Index
...
`

Sum Of Odd And Even Digits In Number


If you need explanation Read this topic

If you need Answer Take test on this topic

Sum Of Odd And Even Digits In Number
What will be the output of the following program?
public class SumOfOddAndEvenDigitsInNumber
{
    public static void main(String args[])
    {
        int number = 1235689012;
        int value1 = 0;
        int value2 = 0;
        while (number > 0)
        {
            if ((number % 10) % 2 == 0)
                value1 = value1 + number % 10;
            else
                value2 = value2 + number % 10;
            number = number / 10;
        }
        System.out.print("Value1 : " + value1 + " Value2 : " + value2);
    }
}

A. Value1 : 18 Value2 : 19
B. Value1 : 10 Value2 : 19
C. Value1 : 28 Value2 : 11
D. Value1 : 31 Value2 : 10
E. Compilation Error or Runtime Error
Topic: Java for loops vs Java while loops vs Java do while loops

If you need explanation Read this topic

If you need Answer Take test on this topic

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

ans a.
value 1=2+0+8+6+2 sum of even nums
value2=1+9+5+3+1 sum of odd
frm givn conditions

Posted by Anand Kumar    2014-08-12 08:47:59


Ans: A  v1 contains sum of even numbers i.e 18 and v2 contains sum of odd no's i.e 19

Posted by Indrasena Reddy    2014-08-12 08:47:59


answer A....    value 1 is sum of even numbers in given number 1235689012....  value2 is  sum of odd numbers.... So value1=2+6+8+0+2=18                             and value2=1+3+5+9+1=19

Posted by Chandra Sekhar Reddy    2014-08-12 10:25:19


A

Posted by Deepika Deepu    2014-08-12 15:41:02


a

Posted by Potti Saikrishna    2014-08-12 15:42:09


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

Posted by Merit Campus    2014-08-13 04:16:39


© meritcampus 2019

All Rights Reserved.

Open In App