How can I display the sum of the digits of a number entered by a user?
37 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Rodha M
el 23 de Sept. de 2020
Comentada: Rodha M
el 23 de Sept. de 2020
I am new to MATLAB, and I want to know how can I display the sum of the digits of a number entered by a user, I started the program but I got stuck.
x=input('Please enter a number: '); %asks the user to enter a number
n= strlength(abs(x) + "") %count the number of digits in that number
y=fprintf('The number of digits of %d is %d \n',x,n);
I would appreciate it if anyone could help. Thank you in advance.
0 comentarios
Respuesta aceptada
David Hill
el 23 de Sept. de 2020
Editada: David Hill
el 23 de Sept. de 2020
n=length(num2str(x));%number of digits
s=sum(num2str(x)-'0');%sum of digits
Más respuestas (2)
KSSV
el 23 de Sept. de 2020
n = 3434;
digits = dec2base(N,10) - '0' ;
iwant = sum(digits)
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!