java program to find the difference between greatest and smallest digits presents in the number

Programming Example

Write a java program to accept an integer number to find out and print the difference between greatest and smallest digits presents in the number .


Solution

import java.util.*;
class Difference
{
    public static void main(String arr[]) 
    {
        Scanner sc=new Scanner(System.in);
        int min=9,max=0,number,digit,diff;
        System.out.println("Enter any Integer Number : ");
        number=sc.nextInt();
        while(number>0)
        {
            digit=number%10;
            if(min>digit)
            {
                min=digit;
            }
            if(max<digit)
            {
                max=digit;
            }
            number=number/10;
        }
        diff=max-min;
        System.out.println("Greatest  Digit : "+max);
        System.out.println("Smallest  Digit : "+min);
        System.out.println("Difference  Between Greatest and Smallest  Digit : "+diff);        
    }
}
Output

Enter any Integer Number : 

23478

Greatest  Digit : 8

Smallest  Digit : 2

Difference  Between Greatest and Smallest  Digit : 6

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Website development Company in Allahabad