n0s3c

MainClass.java

Dec 4th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class MainClass {
  4.  
  5.     static int UserInput;
  6.  
  7.     public static void main(String[] args) {
  8.         System.out.print("Enter the number: ");
  9.         Scanner s = new Scanner(System.in);
  10.         UserInput = s.nextInt();
  11.         boolean FinalResult = IsPalindrome(UserInput);
  12.         if (FinalResult == true) {
  13.             System.out.println(UserInput + " Is a Palindrome.");
  14.         } else if (FinalResult == false) {
  15.             System.out.println(UserInput + " Is NOT a Palindrome.");
  16.         }
  17.     }
  18.  
  19. //Function for reversing string entries.
  20.     static String ReverseString(String value) {
  21.         byte[] strAsByteArray = value.getBytes();
  22.         byte[] result = new byte[strAsByteArray.length];
  23.         // Store result in reverse order into the
  24.         // result byte[]
  25.         for (int i = 0; i < strAsByteArray.length; i++) {
  26.             result[i] = strAsByteArray[strAsByteArray.length - i - 1];
  27.         }
  28.         return new String(result);
  29.     }
  30.  
  31. //Function for converting a numeric value to String.
  32.     static String IntToString(int value) {
  33.         return String.valueOf(value);
  34.     }
  35.  
  36. //Function for converting a String value to a number.
  37.     static int StringToInt(String value) {
  38.         return Integer.parseInt(value);
  39.     }
  40.  
  41. //Function for checking if the number is palindromic.
  42.     static boolean IsPalindrome(float value) {
  43.         String IntString = IntToString(UserInput);
  44.         String ReversedValue = ReverseString(IntString);
  45.         return ReversedValue.equals(IntToString(UserInput));
  46.  
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment