clc
clear
isbn = input('Enter a nine digit ISBN number:' );
isbn = char(string(isbn));
if strlength(string(isbn)) == 9
d1 = str2double(isbn(1));
d2 = str2double(isbn(2));
d3 = str2double(isbn(3));
d4 = str2double(isbn(4));
d5 = str2double(isbn(5));
d6 = str2double(isbn(6));
d7 = str2double(isbn(7));
d8 = str2double(isbn(8));
d9 = str2double(isbn(9));
A = (d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9);
d10 = mod (A, 11);
if d10 ~= 10
fprintf ("ISBN-10: %d-%d%d-%d%d%d%d%d%d-%d\n",d1, d2, d3, d4, d5, d6, d7, d8, d9, d10);
elseif d10 == 10
d10 = 'X';
fprintf ("ISBN-10: %d-%d%d-%d%d%d%d%d%d-%s\n",d1, d2, d3, d4, d5, d6, d7, d8, d9, d10);
end
else
if length(isbn) ~= 9
disp ('Error, ISBN must be exactly 9 digits.')
end
end
Best Answer