Calculate sum and check last digit even or odd in Java
if else - Question 8
In this question, we will see how to input 2 numbers and check if the sum of their last digit is even or odd in Java programming using the if else statement. To know more about if else statement click on the if else statement lesson.
Q8) Write a program in Java to input 2 numbers and check if the sum of their last digit is even or odd.
Program
import java.util.Scanner;
public class Q8
{
public static void main(String args[])
{
int a,b,s;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 2 numbers");
a=sc.nextInt();
b=sc.nextInt();
s=(a%10)+(b%10);
if(s%2==0)
{
System.out.print("Sum of their last digit is even");
}
else
{
System.out.print("Sum of their last digit is odd");
}
}
}
Output
Enter 2 numbers 54 69 Sum of their last digit is odd