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

Antworten (1)

Azzi Abdelmalek
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
Kristian
Kristian am 28 Dez. 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 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).

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by