C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Check Sum Program - Java Programs

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

 

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 = Integer.parseInt(in.nextLine()); int sum = 0; int smallest = num % 10; int largest = num % 10; for(int i = num; i != 0; i /= 10){ int digit = i % 10; if(smallest > digit) smallest = digit; if(largest < digit) largest = digit; } sum = smallest + largest; if(sum % 2 == 0) System.out.println("Sum is even"); else System.out.println("Sum is odd"); } }

Output

 
OUTPUT 1:
Enter the number: 6425
Sum is even

OUTPUT 2:
Enter the number: 3748
Sum is odd