Java: Check if a positive number is a palindrome or not
Check Palindrome Number
Write a Java program to check if a positive number is a palindrome or not.
Pictorial Presentation:
Sample Solution:
Java Code:
import java.util.*;
public class test {
public static void main(String[] args) {
int num;
// Create a Scanner object for user input
Scanner in = new Scanner(System.in);
// Prompt the user for a positive integer
System.out.print("Input a positive integer: ");
// Read the integer entered by the user
int n = in.nextInt();
// Display a message to check if the number is a palindrome
System.out.printf("Is %d a palindrome number?\n", n);
// Check if the number is a palindrome and print the result
System.out.println(is_Palindrome(n));
}
// Function to reverse the digits of a number
public static int reverse_nums(int n) {
int reverse = 0;
while (n != 0) {
reverse *= 10;
reverse += n % 10;
n /= 10;
}
return reverse;
}
// Function to check if a number is a palindrome
public static boolean is_Palindrome(int n) {
return (n == reverse_nums(n));
}
}
Sample Output:
Input a positive integer: 151 Is 151 is a palindrome number? true
Flowchart:
For more Practice: Solve these Related Problems:
- Modify the program to check if a string is a palindrome.
- Write a program to find the next palindrome number greater than a given number.
- Modify the program to check if a negative number is a palindrome.
- Write a program to count the number of palindromic numbers in a given range.
Go to:
PREV : Rotate String by Offset.
NEXT : FizzBuzz 1 to 100.
Java Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.