1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab-Number Palindrome

  1. Nov 15, 2015 #1
    1. The problem statement, all variables and given/known data
    Check if a number is palindrome or not.

    2. Relevant equations
    Writing down the code in attempt.

    3. The attempt at a solution
    Code (Text):
    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 (Text):
    Enter number: 66
    The reversed number is
       Inf

    The number is not palindrome
    What is the error here?
     
  2. jcsd
  3. Nov 15, 2015 #2

    Mark44

    Staff: Mentor

    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:
    Code (Matlab M):

    x=input('Enter number: ');
    temp=x;
    rev=0;
    while(temp>0)
        a=mod(temp,10);
        rev=rev*10+a;
        temp=temp/10;
    end
     
  4. Nov 15, 2015 #3
    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?
     
  5. Nov 15, 2015 #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
     
  6. Nov 15, 2015 #5

    Mark44

    Staff: Mentor

    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.

    I type [code=matlab] at the top and [/code] at the bottom.
     
  7. Nov 16, 2015 #6
    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 (C):
    #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: Nov 16, 2015
  8. Nov 16, 2015 #7

    DrClaude

    User Avatar

    Staff: Mentor

    That's not correct. temp = 0.6

    Because you declared temp as an int. There is no such thing in Matlab, so temp will store the floating-point result.
     
  9. Nov 16, 2015 #8
    So how can I correct that in matlab?
     
  10. Nov 16, 2015 #9

    DrClaude

    User Avatar

    Staff: Mentor

    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.
     
  11. Nov 16, 2015 #10
    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.
     
  12. Nov 16, 2015 #11

    DrClaude

    User Avatar

    Staff: Mentor

    Yes. I meant >= 1.
     
  13. Nov 16, 2015 #12

    Mark44

    Staff: Mentor

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Matlab-Number Palindrome
  1. Counting Palindromes (Replies: 3)

  2. Matlab palindrome (Replies: 3)

Loading...