Write a Java method to check numbers is palindrome number or not


Introduction

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. import java.util.Scanner;
  2.  
  3. public class Javaexcercise
  4. {
  5. public static void main(String[] args)
  6. {
  7. Scanner console = new Scanner(System.in);
  8.  
  9. int num;
  10.  
  11. System.out.print("Enter any number: ");
  12. num = console.nextInt();
  13.  
  14. if(isPalindrom(num))
  15. {
  16. System.out.println("Number is palindrom");
  17. }
  18. else
  19. {
  20. System.out.println("Number is not palindrom");
  21. }
  22. }
  23.  
  24.  
  25. public static boolean isPalindrom(int number)
  26. {
  27. int temp = number;
  28. int remainder;
  29. int reverse = 0;
  30.  
  31. while(temp > 0)
  32. {
  33. remainder = temp % 10;
  34. temp = temp / 10;
  35. reverse = reverse * 10 + remainder;
  36. }
  37.  
  38. return reverse == number;
  39. }
  40. }

Result

java techstudy.org

     





Related Topics




Popular Posts