How to add individual digits

Hello I want to get a 9 digit number from the user and add them all up together; for example, user encodes "222222222" then this should add up to 18 but my code doesnt show that. Here is what I have done:
Userinput = input('Please enter a code to break:','s');
if strlength(Userinput) ~= 9
disp ('Decoy Message: Not a nine-digit number.');
end
v = Userinput(1)+Userinput(2)+Userinput(3)+Userinput(4)...
+Userinput(5)+Userinput(6)+Userinput(7)+Userinput(8)+Userinput(9);
disp(v);

Answers (1)

U = sum(input('Please enter a code to break:','s')-'0');

Asked:

on 8 Feb 2021

Answered:

on 8 Feb 2021

Community Treasure Hunt

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

Start Hunting!