KnowledgeBoat Logo
|

Computer Applications

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:64253748
Largest digit:68
Smallest digit:23
Sample Output:Sum is evenSum is odd

Java

Java Iterative Stmts

16 Likes

Answer

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");
    }
}

Output

BlueJ output of 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.BlueJ output of 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.

Answered By

7 Likes


Related Questions