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

  • Thread starter Thread starter Raghav Gupta
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around a MATLAB implementation for checking if a number is a palindrome. Participants share their code attempts, results, and errors encountered during execution, focusing on the handling of variable types and the logic of the palindrome-checking algorithm.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • Participants present a MATLAB code snippet to check for palindromic numbers and report an issue where the reversed number is displayed as 'Inf'.
  • Some participants suggest that the issue may stem from MATLAB treating variables as doubles rather than integers, which could affect the division operation.
  • One participant proposes hand-simulating the code to understand the flow and identify errors.
  • Another participant mentions that using printf() statements could help debug the values of variables during execution.
  • There is a discussion about the difference in behavior between MATLAB and C++, particularly regarding variable types and division operations.
  • Suggestions are made to use the floor function to replicate C++ behavior in MATLAB, with some participants agreeing that this resolves the issue.
  • However, there is contention regarding the condition in the while loop, with differing opinions on whether it should check for 'temp > 1' or 'temp >= 1'.
  • One participant recommends using the div operator for integer division in MATLAB as a potential solution.

Areas of Agreement / Disagreement

Participants express differing views on the correct implementation of the palindrome-checking logic, particularly regarding variable types and loop conditions. There is no consensus on the optimal solution, as multiple approaches are discussed.

Contextual Notes

Participants note that MATLAB's handling of variable types differs from C++, which may lead to unexpected results in calculations. The discussion includes unresolved mathematical steps and assumptions about variable behavior.

Who May Find This Useful

Readers interested in MATLAB programming, palindrome algorithms, debugging techniques, and comparisons between programming languages may find this discussion relevant.

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;
count<<" Enter a number\n";
cin>>x;
temp=x;
while( temp> 0){
a = temp%10;
rev= rev*10 + a;
temp= temp/10;
}
if(rev==x)
count<<"\nThe number is palindrome";
else
count<<"\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   Reactions: DrClaude

Similar threads

  • · Replies 3 ·
Replies
3
Views
3K
Replies
55
Views
7K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 12 ·
Replies
12
Views
10K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K