Coding Interview

coding interview questions part 2

Pinterest LinkedIn Tumblr

Palindrome Number

A palindromic number is a number that remains the same when its digits are reversed.
Example:-
55, 66, 77, 88, 99 are Palindrome numbers
23, 45, 90 not palindrome numbers

Source Code

package com.dsacode.Probelms;

public class Palindrome {
	
	private int reverse(int number){
		int res  = 0;
		
		while(number > 0){
			res = res * 10 + number %10;
			number = number /10;
		}
		return res;
	}
	
	public boolean isPalindrome(int num){
		
		if(num == reverse(num))
			return true;
		
		return false;
	}
	
	public static void main(String[] args) {
		
		Palindrome p = new Palindrome();
		
		System.out.println("Check 11 is polindrome:"+ p.isPalindrome(11));
		
		System.out.println("Check 100 is polindrome:"+p.isPalindrome(100));

	}

}
#include "stdafx.h"

#include < iostream >
using namespace std;

int reverse(int number){
	int res = 0;

	while (number > 0){
		res = res * 10 + number % 10;
		number = number / 10;
	}
	return res;
}

bool isPalindrome(int num){

	if (num == reverse(num))
		return true;

	return false;
}

int _tmain(int argc, _TCHAR* argv[])
{
	cout << "Check 11 is polindrome:" << isPalindrome(11) << endl;

	cout << "Check 100 is polindrome:" << isPalindrome(100) << endl;


	return 0;
}

Output

Check 11 is polindrome:true
Check 100 is polindrome:false

Algorithm Explanation

Read the number and send to reverse the number.
Compare reverse return with the original number.
If both are same, palindrome number.
1 2 3 4 5

Write A Comment