How can I get a desired input? Also, why does 1 = 49 and 2 = 50 etc. Anyway to keep there values?
1 view (last 30 days)
Show older comments
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 Comments
Answers (1)
Azzi Abdelmalek
on 26 Dec 2014
N=input('Enter your student number:');
if N>0
disp('Sum of digits:')
disp(sum(num2str(N)-'0'))
end
2 Comments
Image Analyst
on 28 Dec 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).
See Also
Categories
Find more on Large Files and Big Data 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!