Java Tutorial
In java programming, this program is used to accept a number as user input and checks whether number is palindrome or not.
Number is palindrome number if number and reverse of the number looks equal, otherwise not palindrome number.
import java.io.*; public class Palindrome { public static boolean checkPolindrome(int number){ int num = 0, digit, revNumber=0; num = number; while (num != 0){ digit = num % 10; revNumber = (revNumber * 10) + digit; num = num / 10; } if (number == revNumber){ return true; } return false; } public static void main(String[] args){ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int number; System.out.println("Check Polindrome or not"); System.out.println("---------------------"); System.out.print("Enter Number: "); try{ number = Integer.parseInt(br.readLine()); if (checkPolindrome(number)){ System.out.println("Number '"+number+"' is Palindrome!"); } else { System.out.println("Number '"+number+"' is not Palindrome!"); } } catch(IOException ioe){ System.out.println("Exception: "+ioe); } } }Output:
D:\Java_Programs>javac Palindrome.java D:\Java_Programs>java Palindrome Check Polindrome or not --------------------- Enter Number: 101 Number '101' is Palindrome! D:\Java_Programs>javac Palindrome.java D:\Java_Programs>java Palindrome Check Polindrome or not --------------------- Enter Number: 123 Number '123' is not Palindrome!
import java.io.*; public class Palindrome { public static boolean checkPolindrome(int number){ int num = 0, digit, revNumber=0; num = number; do { digit = num % 10; revNumber = (revNumber * 10) + digit; num = num / 10; } while (num != 0); if (number == revNumber){ return true; } return false; } public static void main(String[] args){ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int number; System.out.println("Check Polindrome or not"); System.out.println("---------------------"); System.out.print("Enter Number: "); try{ number = Integer.parseInt(br.readLine()); if (checkPolindrome(number)){ System.out.println("Number '"+number+"' is Palindrome!"); } else { System.out.println("Number '"+number+"' is not Palindrome!"); } } catch(IOException ioe){ System.out.println("Exception: "+ioe); } } }
import java.io.*; public class Palindrome { public static boolean checkPolindrome(int number){ int num = 0, digit, revNumber=0; num = number; for (num=number; (num != 0) ; num=num/10){ digit = num % 10; revNumber = (revNumber * 10) + digit; } if (number == revNumber){ return true; } return false; } public static void main(String[] args){ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int number; System.out.println("Check Polindrome or not"); System.out.println("---------------------"); System.out.print("Enter Number: "); try{ number = Integer.parseInt(br.readLine()); if (checkPolindrome(number)){ System.out.println("Number '"+number+"' is Palindrome!"); } else { System.out.println("Number '"+number+"' is not Palindrome!"); } } catch(IOException ioe){ System.out.println("Exception: "+ioe); } } }
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page