JAVA program to find largest and smallest digit in a number

JAVA program to find largest and smallest digit in a number

In our previous post we have seen How to run java program written in notepad. In this post we are going to discuss about How we can find largest and smallest digit in a number using JAVA program.

Find largest and smallest digit in a number

Output:

Enter any Integer Number: 890123425

——————————————–
Input Number : 890123425
Smallest digit : 0
Largest digit : 9

Description:

  • To run above program smoothly, please provide the integer number.
  • In the above program first  I have taken an integer number as input from user.
  • Stored last digit of input number into smallestNumber, largestNumber using “smallestNumber=largestNumber=number%10;” code.
  • In while loop I am finding the each digit using % operator (modulo operator) and after getting digit comparing it with largest and smallest digit stored at variable smallestNumber, largestNumber, if digit is greater than value at variable largestNumber replace the value at largestNumber with the new digit obtained. And if digit is smallest than value at variable smallestNumber replace the value at smallestNumber with the new digit obtained
  • Next dividing the number by 10 so that after getting first digit it will be removed from original number.
  • Loop will be continue upto number > 0.
  • Finally largest and smallest digit will be obtained and display the result.

Some related programs.

Click here to go Program Gallery.

           

Leave a Reply