Programming Examples

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


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

Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Best Website and Software Company in Allahabad