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.

Answers (1)

% check this
a = 4321;
b=str2num(num2str(a)');
diff = abs(sum(b)- prod(b))

4 Comments

we do not encourage giving full solutions for homework, especially when the person has not made any attempts to solve the problem. Posting a homework question without even any discussion tends to suggest that the person is not interested in anything other than copying the solution without any work.
this is not what i am doing , i already have my solution which i can copy and paste it here if you like , i had a little difficuilty doing it so i wanted to make sure if my soultion is correct or no
You can check if you want !
str=input('enter a number');
X = 0;
Y = 1;
W = str;
while W
digit = mod(W, 10);
X = X + digit;
Y = Y * digit;
W = fix(W/10);
end
answer = Y - X;
disp(answer)
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.

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Products

Release

R2021a

Asked:

on 5 Jun 2021

Commented:

on 5 Jun 2021

Community Treasure Hunt

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

Start Hunting!