write a program to input an integer number and find and print the smallest and largest digit of the number in java
Answers
// Java program to find and print the largest and smallest digit of a number
import java.util.*;
import java.lang.*;
import java.io.*;
class digitLargeSmall
{
// Function to find the largest and smallest digit of a number
static void digitFun(int n)
{
int largest = 0;
int smallest = 9;
while(n != 0)
{
int r = n % 10;
// Find the largest digit
largest = Math.max(r, largest);
// Find the smallest digit
smallest = Math.min(r, smallest);
n = n / 10;
}
System.out.println(largest + " " + smallest);
System.out.println(---------------------------------);
System.out.println(largest + " " + smallest);
}
Answer:
Step-by-step explanation:
import java.util.Scanner;
Class SmallestAndLargestNum
{
public static void main(String[ ] args)
{
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
int largest=0,smallest=9,rem;
while(n!=0)
{
rem=n%10;
if(largest<rem)
{
largest=rem;
}
else if(smallest>rem)
{
smallest=rem;
}
n=n/10;
}
System.out.println(largest+" : is the largest number");
System.out.println(smallest+" : is the smallest number");
}
}