Skip to content
This repository has been archived by the owner on Oct 14, 2021. It is now read-only.

Commit

Permalink
Create PalindromeChecker.java (#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brkgng authored Oct 10, 2021
1 parent dc9a066 commit c8b2e93
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Programming/Java/PalindromeOrNot/PalindromeChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.util.Scanner;

public class PalindromeChecker {

public static void main(String args[]){

// Get number from user
System.out.println("Enter a number : ");
int x = new Scanner(System.in).nextInt();

// Check if x is palindrome
if(isPalindrome(x)){
System.out.println(x + " is a palindrome");
}else{
System.out.println(x + " is not a palindrome");
}
}

// Given an integer x, return true if x is palindrome integer
public static boolean isPalindrome(int x) {
// Negative numbers can not be palindrome
if (x < 0) return false;
// 1-digit numbers are palindrome
if (x < 10) return true;

int palindrome = x;
int reverse = 0;

// The reverse of given x
while (palindrome != 0) {
int remainder = palindrome % 10;
reverse *= 10 + remainder;
palindrome /= 10;
}

// if x and reversed x is equal, return true
if (x == reverse)
return true;
return false;
}
}

0 comments on commit c8b2e93

Please sign in to comment.