8
Define a class to accept a number. Check if the sum of the largest digit and the smallest digit is an even number or an odd number. Print appropriate messages.
| Sample Input: | 6425 | 3748 |
| Largest digit: | 6 | 8 |
| Smallest digit: | 2 | 3 |
| Sample Output: | Sum is even | Sum is odd |
Show SolutionHide Solution↓
import java.util.Scanner;
class Find{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter the number: ");
int num = in.nextInt();
int sum = 0;
int s = 10;
int l = -1;
while (num != 0) {
int d = num % 10;
if (d < s)
s = d;
if (d > l)
l = d;
num /= 10;
}
sum = s + l;
if(sum % 2 == 0)
System.out.println("Sum is even");
else
System.out.println("Sum is odd");
}
}
