Write a MATLAB program for Sum of Digits using while loop

Code:

clear
clc
n = input('Enter the n value : \n');                  
s = 0;                     
while n>0                  
    s = s + mod(n,10); 
    n = floor(n/10);       
end
fprintf('Sum of digit : %d\n',s);

 


Output:


No comments

Powered by Blogger.