what is palindrome number code example

Example 1: palindrome number in java

import java.util.Scanner;

public class Palindrome
{
  public static void main(String args[])
  {
    int num,temp,reverse=0;
    Scanner input=new Scanner(System.in);
    num=in.nextInt();
    temp=num;
    //code to reverse the number
    while(temp!=0)
    {
      int d=temp%10; //extracts digit at the end
      reverse=reverse*10+d;
      temp/=10; //removes the digit at the end
    }
    // 'reverse' has the reverse version of the actual input, so we check
    if(reverse==num)
    {
      System.out.println("Number is palindrome");
    }
    else
    {
      System.out.println("Number is not palindrome");
    }
  }
}

Example 2: formula for finding a mathematical palindrome

how to check if a number is a palindrome or not

Example 3: check if palindrome

function isPalindrome(str) {
  str = str.toLowerCase();
  return str === str.split("").reverse().join("");
}

Example 4: palindrome

function isPalindrome(sometext) {
  var replace = /[.,'!?\- \"]/g; //regex for what chars to ignore when determining if palindrome
  var text = sometext.replace(replace, '').toUpperCase(); //remove toUpperCase() for case-sensitive
  for (var i = 0; i < Math.floor(text.length/2) - 1; i++) {
    if(text.charAt(i) == text.charAt(text.length - 1 - i)) {
      continue;
    } else {
      return false;
    }
  }
  return true;
}
//EDIT: found this on https://medium.com/@jeanpan/javascript-splice-slice-split-745b1c1c05d2
//, it is much more elegant:
function isPalindrome(str) {
  return str === str.split('').reverse().join(''); 
}
//you can still add the regex and toUpperCase() if you don't want case sensitive

Example 5: palindrome

//made by Kashish Vaid the great.
// Palindrome programme using for loop the easiest prgm
#include <stdio.h>
int main() {
    int n, rev = 0, remainder, num;
    printf("Enter an integer: ");
    scanf("%d", &n);
    num = n;

    // reversed integer is stored in rev
  for(num = n ; n!=0 ; n/=10)
{
    remainder = n%10;
    rev = rev*10 + remainder;
}
// if else shortcuts
    ( (rev == num) ? printf("%d is a palindrome.", num) : printf("%d is not a palindrome.", num) );
    return 0;
}
//made by Kashish Vaid the great.

Example 6: check palindrome

bool isPlaindrome(string s)
	{
	   int i=0;
	   int j=s.length()-1;
	   while(i<j)
	   {
	       if(s[i]==s[j])
	       {i++;
	   j--;}
	   else break;
	   }
	   if (i==j || i>j) return 1;
	   else return 0;
	}

Tags:

Java Example