Finding Digits of a Number

2 次查看(过去 30 天)
ceren uçak
ceren uçak 2021-6-1
Given any number, find all the digits of the
number;
For example: 456
-
Digits are 4, 5, 6
WITH LOOPS

回答(1 个)

Asmit Singh
Asmit Singh 2021-6-1
This code basically keeps dividing the number by 10, and uses the remainder as the digit, while the quotient as the new number.
number = 456;
digits = [];
while(number~=0)
remain = rem(number,10);
number = fix(number/10);
digits=[digits remain];
end
digits = fliplr(digits)
  2 个评论
ceren uçak
ceren uçak 2021-6-1
Thank you very much for answering the question.
James Tursa
James Tursa 2021-6-1
@Asmit Singh Please do not post complete answers to homework questions.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by