In this tutorial you will learn how to write a program in Java to check a given number is palindrome or not using iteration.
Before moving directly on the writing the program
Read this: What is Palindrome Number? Write a palindrome program in C.
How our Java program will behave?
Our program will take a number as an input to check given number of.
Suppose if someone gives an input 121 then our program should print “the given number is a palindrome”.
And if someone given input 123 the our program should print “the given number is not a palindrome number”.
We will also look at the palindrome program for string in this tutorial.
Read this : Palindrome Program in Java using Recursion
Program 1: Palindrome program in Java using Iterative Method
import java.util.*;
class Main{
public static void main(String ...args){
int tempvar,remainder,reverseNum=0;
Scanner sc= new Scanner(System.in);
System.out.print("Enter number- ");
int originalNum= sc.nextInt();
tempvar = originalNum;
while (tempvar != 0) {
remainder = tempvar % 10;
reverseNum = reverseNum * 10 + remainder;
tempvar /= 10;
}
if (originalNum == reverseNum)
System.out.print("Number is Palindrom");
else
System.out.print("Number is not Palindrom");
}
}
Output:
Enter number- 5445
Number is Palindrom
Program 2: Palindrome Program using Java 8 (String Input)
import java.util.stream.IntStream;
public class PalindromeJava8 {
public static void main(String[] args) {
String input = "radar";
String cleanInput = input.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
boolean isPalindrome = IntStream.range(0, cleanInput.length() / 2)
.noneMatch(i -> cleanInput.charAt(i) != cleanInput.charAt(cleanInput.length() - 1 - i));
if (isPalindrome) {
System.out.println("Given input is palindrome.");
} else {
System.out.println("Given input is not palindrome");
}
}
}
Output
Given input is palindrome.
Program 3: Palindrome Program using Java 8 (Integer Input)
import java.util.stream.IntStream;
public class PalindromeJava8 {
public static void main(String[] args) {
int input = 1345431;
String intInput = String.valueOf(input);
boolean isPalindrome = IntStream.range(0, intInput.length() / 2)
.noneMatch(i -> intInput.charAt(i) != intInput.charAt(intInput.length() - 1 - i));
if (isPalindrome) {
System.out.println("Given input is palindrome.");
} else {
System.out.println("Given input is not palindrome");
}
}
}
Output
Given input is palindrome.
Conclusion
In this tutorial, we have seen various ways to write and implementation the palindrome programs in Java and java 8. We have also seen for the both numerical and string inputs.
Key Highlights:
- Iterative Numerical Palindrome Check:
- In first program we have followed an iterative method to determine whether a given numerical input is a palindrome.
- Here we have used a while loop to reverse the digits of the number and compares the original and reversed numbers.
- If Original and Reversed Digit match found, then input is a Palindrome otherwise not and Palindrome number.
- Java 8 Palindrome Check for Strings:
- In second program we have used Java 8 concepts and methods to string is palindrome or not.
- Here we have used streams, lambda expressions, and string manipulations to remove non-alphanumeric characters and perform the palindrome check. For this we have used replaceAll(“[^a-zA-Z0-9]”, “”).
- Here we have are compared first element with last element, if match not found the return false and print not palindrome, Otherwise print palindrome.
- Java 8 Palindrome Check for Integers:
- The third program is very similar to second program and here also we are using the Java 8 approach.
- Here we have converted the integer to a string and applied the same Java 8 logic for palindrome validation.
Hope this tutorial will be helpful to you.
Happy coding! 🚀