Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,

im using matlab and trying to reverse a number, using the code below:


C#
function rnum = reverse(num)
rnum=0;
while(num>0)
    m= mod(num,10);
    rnum = rnum * 10 + m;
    num = num /10;
end

end



i get :

VB
ans =

   Inf


how can i fixe it? and get the true resault?
Posted
Updated 3-Dec-17 19:39pm

A quick google search i found this,

Transform number into string:
num2str(123)

Use fliplr:
fliplr(num2str(123))
 
Share this answer
 
Comments
m.r.m.40 11-Apr-13 18:24pm    
thank you Mr. Dino.

i don't want to use default functions. looking for an algorithm.
DinoRondelly 11-Apr-13 18:25pm    
Ok well you'll probably have to build that, not sure why though ... good luck
at least this one worked:

C#
function rev = reverse(num)
rev=0;
while(num>1)
    m= mod(num,10);
    rev = floor(rev * 10 + m);
    num = num /10;
    %-----display situation each turn
    disp(num);
end
end
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900