FOR FREE MATERIALS

Palindrome Number

Back to Programming

Description

A number is said to be a palindrome number if the number is equal to its reverse. For example, if a number is 121 then the reverse of the number is also 121

Therefore, this number is a palindrome. But if the number is 123, then the reverse of this number is 321. Here, the number and the reverse are not the same. So, it is not a palindrome.

Algorithm

INPUT: A number
OUTPUT: Whether the number is a palindrome or not.
PROCESS:
Step 1: [Taking the input]
	Read n [the number to be checked]
Step 2: [Checking the number]
	Set m<-n
Set rev<-0
[Reversing the number]
While n>0 repeat
	Set rev<-rev×10 + (n mod 10)
		Set n<-n/10
	[End of ‘for’ loop]
	If m=rev then
		Print "The number is palindrome"
	Else
		Print "The number is not palindrome"
	[End of ‘if’]
[End of checking for ‘palindrome’]
Step 3: Stop.

Code

Time Complexity:

 

while(n>0)-----------------------------------O(m)

                {   rev=rev*10+(n%10);

                     n=n/10;    }

The time complexity of checking a number is O(m) where m is the number of digits of the given number.

 

Space Complexity:

The space complexity of this program is O(1) as it requires a constant number of space to execute for any given input.