Write a Java program to check if a positive number is a palindrome 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.*;
  2. public class Javaexcercise {
  3. public static void main(String[] args)
  4. {
  5. Scanner in = new Scanner(System.in);
  6. System.out.print("Enter a integer: ");
  7. int num = in.nextInt();
  8. System.out.printf("Is %d is a palindrome number?\n",num);
  9. System.out.println(palindrome(num));
  10. }
  11.  
  12. private static boolean palindrome(int num) {
  13. String str = String.valueOf(num);
  14. int i = 0;
  15. int j = str.length() - 1;
  16. while (i < j) {
  17. if (str.charAt(i++) != str.charAt(j--)) {
  18. return false;
  19. }
  20. }
  21. return true;
  22. }
  23. }

Result

java techstudy.org

     





Related Topics




Popular Posts