Matlab-Palindrome Checker: Check if a Number is Palindrome or Not

  • Thread starter Thread starter Raghav Gupta
  • Start date Start date
AI Thread Summary
The discussion focuses on troubleshooting a MATLAB code designed to check if a number is a palindrome. The user encounters an issue where the reversed number is displayed as "Inf" when inputting 66, indicating a problem with variable types, as MATLAB treats numbers as floating-point by default. The solution involves using the floor function or the integer division operator to ensure that the division results in an integer, similar to how C++ handles integer division. After implementing these changes, the code functions correctly, confirming that the input number is a palindrome. The conversation emphasizes the importance of understanding data types and operations in MATLAB programming.
Raghav Gupta
Messages
1,010
Reaction score
76

Homework Statement


Check if a number is palindrome or not.

Homework Equations


Writing down the code in attempt.

The Attempt at a Solution


Code:
x=input('Enter number: ');
temp=x;
rev=0;
while(temp>0)
    a=mod(temp,10);
    rev=rev*10+a;
    temp=temp/10;
end
disp('The reversed number is');
disp(rev);
if(rev==x)
    disp('The number is palindrome');
else
    disp('The number is not palindrome');
end

Result I am getting
Code:
Enter number: 66
The reversed number is
   Inf

The number is not palindrome
What is the error here?
 
Physics news on Phys.org
Raghav Gupta said:

Homework Statement


Check if a number is palindrome or not.

Homework Equations


Writing down the code in attempt.

The Attempt at a Solution


Code:
x=input('Enter number: ');
temp=x;
rev=0;
while(temp>0)
    a=mod(temp,10);
    rev=rev*10+a;
    temp=temp/10;
end
disp('The reversed number is');
disp(rev);
if(rev==x)
    disp('The number is palindrome');
else
    disp('The number is not palindrome');
end

Result I am getting
Code:
Enter number: 66
The reversed number is
   Inf

The number is not palindrome
What is the error here?
Using your example of 66, which is clearly a palindromic number, hand simulate your code to see what it is doing. Take a close look at this section:
Matlab:
x=input('Enter number: ');
temp=x;
rev=0;
while(temp>0)
    a=mod(temp,10);
    rev=rev*10+a;
    temp=temp/10;
end
 
Mark44 said:
Using your example of 66, which is clearly a palindromic number, hand simulate your code to see what it is doing. Take a close look at this section:
Matlab:
x=input('Enter number: ');
temp=x;
rev=0;
while(temp>0)
    a=mod(temp,10);
    rev=rev*10+a;
    temp=temp/10;
end
The thing is that if the datatype of all the variables is int then it works perfectly fine , the method.
In MATLAB we cannot declare variable datatype unlike c++. That should be the error as MATLAB is taking in account double datatype?
 
Also How can I write code of MATLAB here in PF like you have did?
I don't see MATLAB code language in toolbar
 
Raghav Gupta said:
The thing is that if the datatype of all the variables is int then it works perfectly fine , the method.
In MATLAB we cannot declare variable datatype unlike c++. That should be the error as MATLAB is taking in account double datatype?
If you entered 66, and MATLAB said that the reversed number is Inf, then it doesn't seem that you code is working.

Did you try what I suggested, of going through your code by hand to see what it produces?

Alternatively, you can add printf() statements in your code to see the values of a, rev, and temp in the loop.

Raghav Gupta said:
Also How can I write code of MATLAB here in PF like you have did?
I don't see MATLAB code language in toolbar
I type
Matlab:
 at the top and
at the bottom.
 
Mark44 said:
If you entered 66, and MATLAB said that the reversed number is Inf, then it doesn't seem that you code is working.

Did you try what I suggested, of going through your code by hand to see what it produces?
Yes I went through by hand and did operation on 66.
Let's say x=66
then temp= x = 66.
rev =0
Loop starts stating that If temp is greater then zero then continue loop.
a= mod(66, 10) = 6
rev= rev*10 + a= 0*10 + 6 =6
temp= temp/10= 66/10 = 6.
Since, temp = 6>0 loop again runs
a= mod(6,10) = 6
rev = 6*10 + a = 60 + 6=66
temp = 6/10 = 0. And then loop stops.
So finally value of rev should be 66. But then also why my code is showing rev as inf?
I tried this program in C++ with same logic and there it was running perfectly well
Code:
#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
int x, temp, rev=0, a;
cout<<" Enter a number\n";
cin>>x;
temp=x;
while( temp> 0){
a = temp%10;
rev= rev*10 + a;
temp= temp/10;
}
if(rev==x)
cout<<"\nThe number is palindrome";
else
cout<<"\nThe number is not palindrome";
getch();
}
 
Last edited:
Raghav Gupta said:
temp = 6/10 = 0.
That's not correct. temp = 0.6

Raghav Gupta said:
I tried this program in C++ with same logic and there it was running perfectly well
Because you declared temp as an int. There is no such thing in Matlab, so temp will store the floating-point result.
 
DrClaude said:
That's not correct. temp = 0.6Because you declared temp as an int. There is no such thing in Matlab, so temp will store the floating-point result.
So how can I correct that in matlab?
 
To duplicate the behavior of C++, use temp = floor(temp/10). I think that changing the while to while(temp > 1) is a more elegant solution.
 
  • #10
DrClaude said:
To duplicate the behavior of C++, use temp = floor(temp/10).
Yes Now the program is working properly. Floor function works as a greatest integer function here.
DrClaude said:
I think that changing the while to while(temp > 1) is a more elegant solution.
No it is not since when we give input as 121 , the reversed number comes as 12.
 
  • #11
Raghav Gupta said:
Yes Now the program is working properly. Floor function works as a greatest integer function here.

No it is not since when we give input as 121 , the reversed number comes as 12.
Yes. I meant >= 1.
 
  • #12
Raghav Gupta said:
So how can I correct that in matlab?
Use div, the MATLAB integer division operator. See http://www.mathworks.com/help/symbolic/mupad_ref/div.html
 
  • Like
Likes DrClaude

Similar threads

Replies
3
Views
3K
Replies
17
Views
2K
Replies
4
Views
3K
Replies
4
Views
2K
Replies
10
Views
2K
Replies
1
Views
2K
Replies
6
Views
2K
Back
Top