Finding Digits of a Number

조회 수: 11 (최근 30일)
ceren uçak
ceren uçak 2021년 6월 1일
댓글: James Tursa 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에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by