Showing posts with label largest-and-smallest-digit-of-a-number. Show all posts
Showing posts with label largest-and-smallest-digit-of-a-number. Show all posts

Friday, July 31, 2020

Write a program to input a numbers and find the Smallest and Largest Digit of this number.(2784, smallest digit -2, largest digit -8)

62)  Write a program to input a numbers and find the Smallest and Largest Digit of this number.(2784, smallest digit -2, largest digit -8)


import java.util.*;
public class Smallest_Largest_digit
{
    public static void main(String args[ ])
   {
              Scanner sc = new Scanner (System.in);
              System.out.println("Enter a number");
              int a = sc.nextInt();
              int b = 9;
              int c = 0;
              while(a>0)
              {
                  int d = a%10;
                  if(d>c)
                  {
                      c=d;
                  }
                  else if(d<b)
                  {
                      b=d;
                  }
                  a=a/10;
              }
              System.out.println("Smallest digit = "+b);
              System.out.println("Largest digit = "+c);
   }
}

Output : 




 jhvghvgk ;nkjn ,nkjhj