C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Even Pal Program - Java Programs

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 even number.

import java.util.Scanner; class EvenPal{ public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.print("Enter the number: "); int n = Integer.parseInt(in.nextLine()); int r = reverse(n); int s = sum(n); if(n == r && s % 2 == 0) System.out.println("EvenPal number!"); else System.out.println("Not an EvenPal number."); } public static int reverse(int n){ int rev = 0; for(int i = n; i != 0; i /= 10) rev = rev * 10 + i % 10; return rev; } public static int sum(int n){ int s = 0; for(int i = n; i != 0; i /= 10) s += i % 10; return s; } }

Output

 
 OUTPUT 1: 
Enter the number: 121
EvenPal number! 

 OUTPUT 2: 
Enter the number: 135
Not an EvenPal number.