Java Palindrome

  1. Palindrome - String Value in Java
  2. Palindrome - Integer Value in Java
  3. Palindrome - Float Value in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Palindrome - String Value in Java


Palindrome_Ex1.java

import java.util.Scanner;
 
public class Palindrome_Ex1 {
	
	void check(String name) {
		
		char[] c1 = new char[name.length()];		
		String rev = "";
				
		for(int i=0; i<name.length(); i++) {
			
			c1[i] = name.charAt((name.length()-1) - i);
			rev = rev + c1[i];
		}
		
		if(name.equals(rev)) {
			System.out.println(name + " is Palindrome");
		}
		else {
			System.out.println(name + " is not Palindrome");
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Palindrome_Ex2 obj = new Palindrome_Ex2();
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Enetr the String :");
		String name = scan.nextLine();
		
		obj.check(name);
	}
}

Sample Output

Enetr the String :
mom
mom is Palindrome
Enetr the String :
Mom
Mom is not Palindrome




B. Palindrome - Integer Value in Java


Palindrome_Ex2.java

import java.util.Scanner;
 
public class Palindrome_Ex2 {
	
	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Enter the Number : ");
		
		int num= Integer.parseInt(scan.nextLine());
		
		int n = num;
		int rev=0;
		
		for (int i=0; i<=num; i++) {
			
			int r = num % 10;
			num = num / 10;
			rev = (rev * 10) + r;
			i = 0;
		}
			
		if(n == rev){
			System.out.println(n + " is Palindrome");
		}
		else{
			System.out.println(n + " is not Palindrome");
		}
	}
} 

Sample Output

Enter the Number : 
1234321
1234321 is Palindrome
Enter the Number : 
123
123 is not Palindrome




C. Palindrome - Float Value in Java


Palindrome_Ex3.java

import java.util.Scanner;
 
public class Palindrome_Ex3 {
 
	float number;
	
	void getVal() {
				
		System.out.println("Enter the number : ");
		Scanner scan = new Scanner(System.in);
		
		number = Float.parseFloat(scan.nextLine());
	}
	
	void divide() {
		
		float temp = number;
		String st = String.valueOf(temp);
				
		String str[] = st.split("\\.");
        
        	int before = Integer.parseInt(str[0]);
        	int after = Integer.parseInt(str[1]);
        
        	String str1 = String.valueOf(revere(before));
        	String str2 = String.valueOf(revere(after));
        
        	if(str1.equals(str[0]) && str2.equals(str[1])) 
        		System.out.println(number + " is a Palindrome");
        	else
        		System.out.println(number + " is not a Palindrome");
	}
 
	private int revere(int value) {
		
		int mod;
		int sum = 0;
        
        	while(value != 0) {
			
			mod = value % 10;
			sum = sum * 10 + mod;
			value = value / 10;
		}
                
        	return sum;
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Palindrome_Ex3 obj = new Palindrome_Ex3();
		
		obj.getVal();
		obj.divide();
	}
}

Sample Output

Enter the number : 
121.363
121.363 is a Palindrome
Enter the number : 
121.369
121.369 is not a Palindrome






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com