Palindrome Numbers | MATLAB Tutorial
by SAURAB S
What is a palindrome number?
A palindrome is any number that when reversed remains same as it was before being reversed.
x=input('Enter the number : '); s=x; res=0; while(s~=0) rem= mod(s,10) res= res*10 + rem; s = floor(s/10) ; end if res==x fprintf('%d is a palindrome number',x) fprintf('\n') else fprintf('%d is not a palindrome number',x) fprintf('\n') end
- The user is asked to enter the number which they want to check.
- The number is stored in a variable.After that, each of the digits from one’s place to the left is taken.
- This is done by using the function named “modulo”.The number is divided by 10 and the remainder is taken.
- The user uses a while loop for extracting the digits one by one.
- After each number is extracted, it is stored in a variable and it is stored in such a way that it occupies the leftmost part initially and then each number gets stored to the right thereafter.
- The while loop should stop when no number can be extracted.
- This newly stored number and our original number are then checked.
- If both are equal then we print “It’s a Palindrome”
- Else we then print that “It’s not”
This code involves reversing the number first.Secondly, using if statement to check whether the inputted number and the reversed number are same.
Following are some of the output for the code
- When we enter the number “1001” we get
- If the entered number is like “34353345”, we get
- When we enter “3456543”, we get