Menu
Topics Index
...
`

Minimum and Maximum Digits in a Number


If you need explanation Read this topic

If you need Answer Take test on this topic

Minimum and Maximum Digits in a Number
What will be the output of the following program?
public class MinMaxDigitInNumber
{
    public static void main(String args[])
    {
        int number = 1235689012;
        range(number);
    }
    private static void range(int number)
    {
        int value1 = 0;
        int value2 = 0;
        for (value1 = value2 = number % 10; number > 0; number = number / 10)
        {
            if (value1 > number % 10)
            {
                value1 = number % 10;
            }
            if (value2 < number % 10)
            {
                value2 = number % 10;
            }
        }
        System.out.print("Value1 : " + value1 + "  Value2 : " + value2);
    }
}

A. Value1 : 8  Value2 : 9
B. Value1 : 1  Value2 : 2
C. Value1 : 0  Value2 : 9
D. Value1 : 9  Value2 : 0
E. Compilation Error or Runtime Error
Topic: Java Methods - Parameter Passing And Scope

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.

Answer is C,
The minimum value in the number "1235689012" is 0 so this is assigned to value1 and the maximum number is 9 so this is assigned to value2.

Posted by Amar Kumar Biradar    2014-02-10 09:21:16


Ans:D values1:9 it checks greatest no among all the digits value2:0 it checks the least no among all the digits

Posted by Indrasena Reddy    2014-02-10 09:23:07


answer is c,for loop is executed 3 times.after first 2 iterations if block of value1 is executed then  value1 =0.In third iteration if block of value2 is executed then value2=9.The final output is value1 : 0 value2 : 9

Posted by Praveen Kumar    2014-02-10 09:25:34


ans e: integer cant hold such a big value.

Posted by Neelima Nadikuda    2014-02-10 09:42:01


Ans:D ,Compilation error because 12345689012 is greater than "int" range.

Posted by Harish Kumar G    2014-02-10 09:44:33


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

Posted by Merit Campus    2014-02-11 03:42:22


Thanks Merit Campus ;)
Got Recharge :)

Posted by Amar Kumar Biradar    2014-02-11 04:23:45


© meritcampus 2019

All Rights Reserved.

Open In App