Skip to content

Instantly share code, notes, and snippets.

@n0toose
Created November 21, 2021 15:28
Show Gist options
  • Save n0toose/2c5cc70f0a7f6c8248bc0a1ef05e9a92 to your computer and use it in GitHub Desktop.
Save n0toose/2c5cc70f0a7f6c8248bc0a1ef05e9a92 to your computer and use it in GitHub Desktop.
Find the sum of digits in MATLAB (Example: Numbers with a sum of digits equals to 6)
count_equal_to_6 = 0;
for times = 1:1000
if sumOfDigits(times) == 6
display(times)
count_equal_to_6 = 1 + count_equal_to_6;
end
end
fprintf('Total: %d\n', count_equal_to_6)
function p = sumOfDigits(n)
a = 0;
stringVar = num2str(n);
for times = 1:strlength(num2str(n))
num = num2str(stringVar(times))-48;
a = a + num;
end
p = a;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment