How to reverse a number

Hi, I want to reverse a number, not a vector, like 8549 to 9458. I used fliplr , or flipud, or y = x(end:-1:1), They just work for row vector. Please, help. Thank you

Réponses (8)

Azzi Abdelmalek
Azzi Abdelmalek le 1 Déc 2012
Modifié(e) : Azzi Abdelmalek le 1 Déc 2012

6 votes

a=8549
out=str2num(fliplr(num2str(a)))

3 commentaires

Aritra Chatterjee
Aritra Chatterjee le 14 Sep 2013
Great!!! Great!!!!
János Stenszky
János Stenszky le 14 Mai 2018
Thanks!
Uchechukwu Obimma
Uchechukwu Obimma le 11 Mai 2020
Thanks

Connectez-vous pour commenter.

Roger Stafford
Roger Stafford le 21 Nov 2014

3 votes

@Jessica: Let x be a non-negative integer. Then y will have the decimal digits of x in reversed order.
y = 0;
while x > 0
t = mod(x,10);
y = 10*y+t;
x = (x-t)/10;
end

2 commentaires

Kunal  Kabi
Kunal Kabi le 8 Juin 2017
Thank you sir because of you i got the idea @RogerStafford.
ABHAS BHATIA
ABHAS BHATIA le 14 Jan 2018
Thanks for the solution @Jessica

Connectez-vous pour commenter.

Kunal  Kabi
Kunal Kabi le 8 Juin 2017
Modifié(e) : Jan le 28 Août 2019

2 votes

If you want to find palindrome numbers between two range here is the answer
clc;
clear all;
close all;
num=input('Enter first range:');
num1=input('Enter second range:');
for i=num:num1
temp=i;
reverse=0;
while temp > 0
t = mod(temp,10);
reverse = 10*reverse+t;
temp = (temp-t)/10;
end
if i==reverse
fprintf('Number is palindrome. %d\n',reverse);
end
end
Jan
Jan le 14 Jan 2018
Modifié(e) : Jan le 14 Jan 2018

1 vote

A vectorized version of Roger's method:
n = 8549;
d = 10 .^ (0:floor(log10(n))); % [1 10, 100, 1000]
v = rem(floor(n ./ d), 10); % The vector of digits
r = v * flip(d).' % Vector multiplication with [1000, 100, 10, 1]
José-Luis
José-Luis le 1 Déc 2012

0 votes

A vectorized, faster alternative
For integers:
your_answer = flipud(sscanf(fliplr(sprintf('%d ',a)),'%d '));
And floating point:
your_answer = flipud(sscanf(fliplr(sprintf('%f ',a)),'%f '));
Example:
a=randi(115422,10000,1);
your_val = flipud(sscanf(fliplr(sprintf('%d ',a)),'%d '));
Jessica
Jessica le 21 Nov 2014

0 votes

what if you aren't allowed to use any string variables, string related function, digitrevorder() and fliplr()??

1 commentaire

Andrew Reibold
Andrew Reibold le 21 Nov 2014
Then you can do divide by 10 tricks with rounding to save each digit, then rearrange after.

Connectez-vous pour commenter.

Kunal  Kabi
Kunal Kabi le 8 Juin 2017

0 votes

Here is your answer
clc;
clear all;
close all;
num=input('Enter a number:');
num1=num;
reverse=0;
while num1 > 0
t = mod(num1,10);
reverse = 10*reverse+t;
num1 = (num1-t)/10;
end
reverse
num
if num==reverse
disp('Number is palindrome.');
else
disp('Number is not palindrome');
end
ANIRUDDHA
ANIRUDDHA le 6 Sep 2022

0 votes

  1. Using function statement, find out the sum of the inverse of any two numbers, a, b. Choose any value of a and b to run program?

1 commentaire

Walter Roberson
Walter Roberson le 6 Sep 2022
This does not appear to be an answer to the question that was asked.

Connectez-vous pour commenter.

Catégories

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by