Home » Programming » Java Interview Programs » Java program for a palindrome and reverse string or number

Java program for a palindrome and reverse string or number

In this article let us see the different ways to reverse the string or number in Java and how to check the given string or number is a palindrome in Java.

First, let us see how to reverse the number by using the below different ways in Java.

Method 1

Java program to reverse the number using while loop

In the below java program I have used integer to declare the number and used while loop to reverse the number.

The technique used here is modulo (or) remainder operator (%) in java. This operator always returns the remainder of the two numbers after division. For example, 28%10 produce the result as 8 and it is a remainder of 28/10.

public class PalindromeReverseNumber {	
//using while loop to reverse the number
private static int reverseNumber(int n) {		
int total = 0,r;		
while(n>0){
	r = n%10;
	total = total*10+r;
	n=n/10;		
}		
return total;
}	
public static void main(String[] args) {		
int n = 123456789;			
System.out.println((reverseNumber(n)));
}	
}

Input = 123456789 & Output = 987654321

Method 2

Java Program to reverse the number using For loop

In the below java program I have used integer to declare the number and used for loop to reverse the number.

public class PalindromeReverseNumber {	
//using for loop to reverse the number
private static int reverseNumber(int n) {		
	int total = 0,r;		
	for(;n!=0;n=n/10){
	   r = n%10;
	   total = total*10+r;			
	}		
	return total;		
}
public static void main(String[] args) {		
	int n = 123456789;		
	System.out.println((reverseNumber(n)));
}
}

Input = 123456789 & Output = 987654321

Another way of using for loop to reverse the number is below.

public class PalindromeReverseNumber {		
//using for loop to reverse the number
private static int reverseNumber(int n) {		
	int total = 0,r;		
	for(;n!=0;){
	  r = n%10;
	  total = total*10+r;		
	  n=n/10;		
	}		
	return total;
}
public static void main(String[] args) {		
	int n = 123456789;			
	System.out.println((reverseNumber(n)));
}
}

Input = 123456789 & Output = 987654321

All the programs we have seen above are used Integer to reverse the number. So we can reverse the number within the Integer range (-2147483648 to 2147483647) in Java. If we want to reverse any bigger number that is beyond the integer range then we have to use BigInteger in Java.

Method 3

Java program to reverse any bigger number using BigInteger

The below java program can reverse any bigger number.

import java.math.BigInteger;
public class PalindromeReverseNumber {		
//using BigInteger and while loop to reverse any bigger number
private static BigInteger reverseNumber(BigInteger n) {		
	BigInteger sum = BigInteger.ZERO,r;		
	while(n.compareTo(BigInteger.ZERO)==1 ? true: false){
		r = n.mod(BigInteger.TEN);
		sum = sum.multiply(BigInteger.TEN).add(r);
		n=n.divide(BigInteger.TEN);		
	}		
	return sum;
}
public static void main(String[] args) {		
	BigInteger n = new BigInteger("123456789123456789");
	System.out.println((reverseNumber(n)));
}	
}

Input = 123456789123456789 & Output = 987654321987654321

Note: All the above programs work only with positive numbers. If you want to reverse both positive and negative numbers then please use the below program.

import java.math.BigInteger;
public class PalindromeReverseNumber {	
//using while loop to reverse both positive and negative number
private static int reverseNumber(int n) {		
	boolean isNegative=false;	
        int total = 0,r;	
	if(n<0){
		n = -1*n;
		isNegative=true;
	}		
	while(n>0){
		r = n%10;
		total = total*10+r;
		n=n/10;		
	}
	if(isNegative)
		return -1*total;
	else
		return total;
}
public static void main(String[] args) {		
	int n = -123456789;	
	System.out.println((reverseNumber(n)));
}	
}

Input = -123456789 & Output = -987654321

Now let us see the below different ways to reverse the string in Java.

Method 1

Java program to reverse the string using for loop

The below java program finds the character of the string from the backward position and concatenates the character one by one to produce the reversed string. Here I have used the charAt() function to find a character of string based on position.

public class PalindromeReverseString {	
//reverse the string using for loop
private static String reverseString(String s){			
	String reversedString = "";
	for(int i=s.length()-1;i>=0;i--){			
		reversedString = reversedString + s.charAt(i);
	}
        return reversedString;
}	
public static void main(String[] args) {		
	String s = "Sachin Tendulkar";		
	System.out.println(reverseString(s));		
}
}

Input = Sachin Tendulkar & Output = rakludneT nihcaS

Method 2

Java program to reverse the string using while loop

public class PalindromeReverseString {		
//reverse the string using while loop
private static String reverseString(String s){			
	String reversedString = "";
	int strLength = s.length()-1;
	while(strLength>=0){	
		reversedString = reversedString + s.charAt(strLength);
		strLength--;
	}
	return reversedString;
}
public static void main(String[] args) {
	String s = "Sachin Tendulkar";		
	System.out.println(reverseString(s));
}
}

Input = Sachin Tendulkar & Output = rakludneT nihcaS

Method 3

Java program to reverse the string using reverse function of StringBuilder

By default, the StringBuilder class has a reverse function in java. We can use this function to reverse any string value. The below java program uses the same to reverse the string.

public class PalindromeReverseString {
//reverse the string using reverse function	
private static String reverseString(String s){		
	StringBuilder s1 = new StringBuilder();
	s1.append(s);
	return s1.reverse().toString();	
}
public static void main(String[] args) {		
	String s = "Sachin Tendulkar";		
	System.out.println(reverseString(s));	
}
}

Input = Sachin Tendulkar & Output = rakludneT nihcaS

Cool!. till now we have seen all the different ways to reverse the string and reverse the number in java. Now let us see how to check the string or number is a palindrome.

Java Program to check the string is palindrome or not

Before writing the program let us know what is palindrome?. A palindrome is a word, number, phrase, or other sets of characters that reads the same backward as forward. For example, radar, level, madam, rotor...

Saying programmatically if the string value and the reversed string value are equal then the string is palindrome otherwise it is not a palindrome. We have seen different ways to reverse the string or number in this article right. We can use any of the above programs for palindrome.

The below java program checks if the given string is palindrome or not.

public class PalindromeReverseString {	
//reverse the string using while loop
private static String reverseString(String s){			
	String reversedString = "";
	int strLength = s.length()-1;
	while(strLength>=0){	
		reversedString = reversedString + s.charAt(strLength);
		strLength--;
	}
	return reversedString;
}	

public static void main(String[] args) {		
	String s = "madam";		
	if(s.equals(reverseString(s)))
		System.out.println("The given string is palindrome");
	else
		System.out.println("The given string is not a palindrome");
}
}

Input = madam & Output = The given string is palindrome

Input = teacher & Output = The given string is not a palindrome.

The below java program checks if the given number is palindrome or not.

public class PalindromeReverseNumber {	
//using while loop to reverse the number
private static int reverseNumber(int n) {		
	boolean isNegative=false;	
        int total = 0,r;	
	if(n<0){
		n = -1*n;
		isNegative=true;
	}			
	while(n>0){
		r = n%10;
		total = total*10+r;
		n=n/10;		
	}
	if(isNegative)
		return -1*total;
	else
		return total;
	}	
public static void main(String[] args) {		
	int n = 1221;		
	if(n==reverseNumber(n))
		System.out.println("The given number is palindrome");
	else
		System.out.println("The given number is not a palindrome");
}	
}

Input = 1221 & Output = The given number is palindrome

Input = 12212 & Output = The given number is not a palindrome

I hope you understood how to reverse the string or number in java and how to check the given string or number is a palindrome in java. Thanks! Keep Reading!.

Leave a Reply

Your email address will not be published. Required fields are marked *