a shot of dev knowledge

Related tags

# How to check if a number is palindrome

A number is a palindrome if it reads the same forwards and backwards. For example, the number 1233321 is a palindrome. ## The algorithm

The algorithm follows 3 steps:

1. Declare two variables: one stores the given number, and the other stores the reversed number.

2. Run the do-while loop until the number of digits in the reversed number are equal to the number of digits in the given number. The reversed number is created by splitting off the end-digit of the given number.

3. Check if the reversed number is equal to the given number.

### Example

Following slides will help you understand a palindrome number.

1 of 5

## Code

The following code checks if the number is palindrome or not:

#include <iostream>
using namespace std;
int main()
{
int copyNumber, givenNumber,
currentDigit, reversedNumber = 0;
givenNumber = 353; //Try changing this
copyNumber = givenNumber;
do
{
currentDigit = copyNumber % 10; //takes the remainder
reversedNumber = (reversedNumber * 10) + currentDigit; //creating the reversed number
copyNumber= copyNumber / 10; //creating new number
} while (copyNumber != 0);
cout << " The reverse of the number is: " << reversedNumber << endl;
if (givenNumber == reversedNumber)
cout << " The number is a palindrome.";
else
cout << " The number is NOT a palindrome.";
return 0;
}

Related tags

RELATED COURSES
Related Courses
Related Courses

#### Keep Exploring

Learn in-demand tech skills in half the time

Copyright ©2021 Educative, Inc. All rights reserved. 