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

  • Thread starter Raghav Gupta
  • Start date
In summary, to correct the code in MATLAB, the integer division operator should be used instead of regular division to avoid getting Inf as the result. Additionally, using the floor function can also help in getting the correct result.
  • #1
Raghav Gupta
1,011
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
  • #2
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
 
  • #3
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?
 
  • #4
Also How can I write code of MATLAB here in PF like you have did?
I don't see MATLAB code language in toolbar
 
  • #5
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 [code=matlab] at the top and [/code] at the bottom.
 
  • #6
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:
  • #7
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.
 
  • #8
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?
 
  • #9
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

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

1. What is a number palindrome in Matlab?

In Matlab, a number palindrome is a number that reads the same forward and backward. For example, 121 is a number palindrome because it reads the same from left to right and right to left.

2. How can I check if a number is a palindrome in Matlab?

You can use the isPalindrome() function in Matlab to check if a number is a palindrome. This function takes in a number as input and returns true if the number is a palindrome, and false if it is not.

3. Can I check if a string is a palindrome using Matlab?

Yes, you can use the isPalindrome() function in Matlab to check if a string is a palindrome. However, you will need to convert the string to a number first using the str2num() function.

4. What is the difference between a number palindrome and a string palindrome in Matlab?

In Matlab, a number palindrome is a number that reads the same forward and backward, while a string palindrome is a string of characters that reads the same forward and backward. They both use the same isPalindrome() function, but the input type is different.

5. How can I find all the palindromic numbers within a given range in Matlab?

You can use a combination of loops and the isPalindrome() function to find all the palindromic numbers within a given range in Matlab. First, you will need to loop through the numbers in the given range and check if each number is a palindrome using the isPalindrome() function. If it is a palindrome, you can store it in an array or print it out.

Similar threads

  • Programming and Computer Science
2
Replies
55
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
9K
Back
Top