Given an integer number n (0<=n<10000), write a code returning the difference between the product of its digits and the sum of its digits.

2 次查看(过去 30 天)
Given an integer number n (0<=n<10000), write a code returning the difference between the product of its digits and the sum of its digits. For instance, if n=432 then the result is 4*3*2-(4+3+2) = 15.

回答(1 个)

SALAH ALRABEEI
SALAH ALRABEEI 2021-6-5
编辑:SALAH ALRABEEI 2021-6-5
% check this
a = 4321;
b=str2num(num2str(a)');
diff = abs(sum(b)- prod(b))
  4 个评论
Walter Roberson
Walter Roberson 2021-6-5
That code seems to work when I test, except for 0 exactly.
For 0 exactly your Y would be 1 and your X would be 0 so you would get 1-0 = 1 as your solution, but for 0 exactly the product of the digits should be 0.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by