How can I get a desired input? Also, why does 1 = 49 and 2 = 50 etc. Anyway to keep there values?
18 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I want someone to enter a 6 digit student number and get the sum of the digits.
What I have so far:
disp('Enter your student number:','s');
if N>0
disp('Sum of digits:')
disp(sum(num2str(N)))
end
0 Kommentare
Antworten (1)
Azzi Abdelmalek
am 26 Dez. 2014
N=input('Enter your student number:');
if N>0
disp('Sum of digits:')
disp(sum(num2str(N)-'0'))
end
2 Kommentare
Image Analyst
am 28 Dez. 2014
They are not entering ASCII values - they're entering the regular characters, but computers deal with numbers so they have to have a number that represents your letters. So '0' is 48 and '1' is 49 and so on. To get 0, 1, etc. you have to subtract 48, which is '0' (which it converts into 48 which is the ASCII value of the character zero).
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!