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
Advertisements
Question
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
Answer in Brief
Advertisements
Solution
import java.util.*;
class EvenPal
{
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a positive number:");
int num=Math.abs(sc.nextlnt());
int rev=0,sum=0, temp=num;
while(temp>0)
{
int r=temp%10;
temp=temp/10;
rev=rev*10+r;
sum+=r;
}
if(rev==num&&sum%2==0
System.out.println("It is an EvenPal
number"+num);
else
System.out.println("It is not an EvenPal
number");
}
}
Output:
Enter a positive number: 121
It is an EvenPal number 121
Enter a positive number: 123
It is not an EvenPal number
shaalaa.com
Using Function Argument
Is there an error in this question or solution?
