How to add individual digits
Show older comments
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)
David Hill
on 8 Feb 2021
U = sum(input('Please enter a code to break:','s')-'0');
Categories
Find more on Image Filtering and Enhancement in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!