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 : 




No comments:

Post a Comment

if you have any doubts, Please let me know.
Your Truly
Shashank Porwal

 jhvghvgk ;nkjn ,nkjhj