Computer Applications

Define a class to accept a number from user and check if it is an EvenPal number or not.

(The number is said to be EvenPal number when number is palindrome number (a number is palindrome if it is equal to its reverse) and sum of its digits is an even number.)

Example: 121 – is a palindrome number
Sum of the digits – 1+2+1 = 4 which is an even number

Java

Java Iterative Stmts

ICSE 2024

6 Likes

Answer

import java.util.Scanner;

public class KboatEvenPal
{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int num = in.nextInt();
        int org = num;
        int rev = 0;
        int sum = 0;
        
        while (num > 0) {
            int d = num % 10;
            rev = rev * 10 + d;
            sum += d;
            num /= 10;
        }
        

        if (org == rev && sum % 2 == 0) {
            System.out.println(org + " is an EvenPal number.");
        } else {
            System.out.println(org + " is not an EvenPal number.");
        }
    }
}

Variable Description Table

Program Explanation

Output

Answered By

1 Like


Related Questions