Write a program to accept four number and check the number palindrome or not. If it is palindrome then display the sum of last two digits else display the product of first two digits of the original number
Answers
Answer:
A simple method for this problem is to first reverse digits of n, then compare the reverse of n with n. If both are same, then return true, else false.
Answer: The required java program is :-
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a four digit number : ");
String num = scan.next();
scan.close();
int num1 = Integer.parseInt(num);
int digit;
StringBuffer numD = (new StringBuffer(num)).reverse();
if(num.equals(numD.toString())) {
int result = 0;
digit = num1%10;
result += digit;
num1 /= 10;
digit = num1%10;
result += digit;
System.out.println("\nThe given number is a palindrome. Sum of last two digits is "+result);
}
else {
int result = 1;
num1 /= 100;
digit = num1%10;
result *= digit;
num1 /= 10;
result *= num1;
System.out.println("\nThe given number is not a palindrome. Product of first two digits of original number is "+result);
}
}
}
Please mark it as Brainliest.