How to reverse a number
조회 수: 28 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (8개)
Azzi Abdelmalek
2012년 12월 1일
편집: Azzi Abdelmalek
2012년 12월 1일
a=8549
out=str2num(fliplr(num2str(a)))
댓글 수: 3
Roger Stafford
2014년 11월 21일
@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
Kunal Kabi
2017년 6월 8일
편집: Jan
2019년 8월 28일
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
댓글 수: 0
José-Luis
2012년 12월 1일
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 '));
댓글 수: 0
Jessica
2014년 11월 21일
what if you aren't allowed to use any string variables, string related function, digitrevorder() and fliplr()??
댓글 수: 1
Andrew Reibold
2014년 11월 21일
Then you can do divide by 10 tricks with rounding to save each digit, then rearrange after.
Kunal Kabi
2017년 6월 8일
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
댓글 수: 0
ANIRUDDHA
2022년 9월 6일
- 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
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!