How would you check if a number is palindrome using a looping algorithm in any programming language ?

A palindrome number is one which remains the same when its digits are reversed. For example 121, if you write 121 backwards its still 121.

The algorithm involves a really simple loop which just reverses the number. The number can then be compared with the original number to check whether they are same or not. If same then the number is palindrome. The code is as follows-

Let 'num' be the variable holding the number and 'copy' be holding a copy of the same number.

Let 'rev' be the variable holding the reverse of the number 'num'. Initial value of 'rev' is 0.

Let 'rem' be the variable holding the remainder of the number at each iteration. Then the loop is-

while(num>0)
{
   rem=num%10;           //takes the last digit of num
   rev=rev*10+rem;       //calculates reverse 
   num=num/10;           //takes num to next digit
}

After this loop finishes we will have the reverse of the number in 'rev' variable. Now this can be compared with 'copy' variable which stores the original number to check if both are same and thus palindrome.


if(rev=copy)
print 'Palindrome'
else
print 'Not Palindrome'

Note that, the 'rev' variable cannot be compared to 'num' variable since the value of 'num' has been modified repetedly by the command 'num=num/10' and now is less than 0 which eventually breaks the loop.

YR
Answered by Yatharth R. Computing tutor

11142 Views

See similar Computing GCSE tutors

Related Computing GCSE answers

All answers ▸

How would 12 be represented in binary?


Show how you would calculate 53 + 39 in 8-bit binary?


Compress the following bit pattern using RLE (Run Length Encoding). 1111 0011 1100 0000


James would like to store a video clip that is 20 frames per second and has a duration of 76 seconds. The resolution of this video is 1280x720 with a colour depth of 24 bits. Calculate the storage requirement for the uncompressed video clip.


We're here to help

contact us iconContact ustelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

MyTutor is part of the IXL family of brands:

© 2025 by IXL Learning