How to add individual digits
Afficher commentaires plus anciens
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);
Réponses (1)
David Hill
le 8 Fév 2021
U = sum(input('Please enter a code to break:','s')-'0');
Catégories
En savoir plus sur Image Filtering and Enhancement dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!