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)
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

Answers (1)

Azzi Abdelmalek
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
Kristian
Kristian on 28 Dec 2014
disp('Enter your student number:');
N = '123456'
disp('Sum of digits:')
disp(sum(num2str(N))- 6*48 )
Got this and it works, but how do I get someone to input the value and make sure the values are not ascii.
Image Analyst
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).

Sign in to comment.

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!