7
Define a class to accept a number and check if the sum of the first digit and the last digit is an even number or an odd number.
Example:
Show SolutionHide Solution↓
N = 2396 N = 9316
First digit: 2 9
Last digit: 6 6
Sum: 8 15
Output: Sum is even Sum is odd
import java.util.Scanner;
public class FirstLastSum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
int lastDigit = n % 10;
int firstDigit = n;
while (firstDigit >= 10) {
firstDigit /= 10;
}
int sum = firstDigit + lastDigit;
System.out.println("First digit: " + firstDigit);
System.out.println("Last digit: " + lastDigit);
System.out.println("Sum: " + sum);
if (sum % 2 == 0)
System.out.println("Sum is even");
else
System.out.println("Sum is odd");
}
}
