Write a program to take input of two decimal numbers, find the sum, then check whether the sum is even or odd.
Answers
Answer:
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("Enter number so it could be added: ");
Scanner input = new Scanner(System.in);
int sum = 0;
int num = input.nextInt();
while (num > 0){
int digit = num % 10;
num /= 10;
sum += digit;
}
if (sum % 2 ==0){
System.out.println("Even");
}
else{
System.out.println("Odd");
}
System.out.println("The Sum of the digits is: " + sum);
}
/*
//this is what i put, but was wrong.
sum = sum + num;
num = num / 10;
System.out.println("Sum of digits: " + sum);
//ends here
*/
Answer:
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("Enter number so it could be added: ");
Scanner input = new Scanner(System.in);
int sum = 0;
int num = input.nextInt();
while (num > 0){
int digit = num % 10;
num /= 10;
sum += digit;
}
if (sum % 2 ==0){
System.out.println("Even");
}
else{
System.out.println("Odd");
}
System.out.println("The Sum of the digits is: " + sum);
}
/*
//this is what i put, but was wrong.
sum = sum + num;
num = num / 10;
System.out.println("Sum of digits: " + sum);
//ends here
*/
{ }
OUTPUT
Enter number so it could be added: 12334
Odd
The Sum of the digits is: 13
Share Improve thi