MATLAB: How to display the sum of all the numbers that passed the “if” statement test

elseendforloopifif statementmatlab functionurgent

This is was I have so far…
prompt = 'What is the original value?' ;
N = input(prompt);
values = 1:N;
for i = (1:N);
x = mod(i ,5);
if x == 0
disp('divisable by 5')
num2str(i)
disp(i)
else disp('not divisable by 5')
end
end
I can access all the numbers that have passed but I don't know how to add them into a sum that doesn't include the user input.

Best Answer

I’m not sure what you want. If you want to store and then sum all the numbers divisible by 5, this is one option:
for i = (1:N);
x = mod(i ,5);
if x == 0
disp('divisable by 5')
div5(i)= i;
num2str(i)
disp(i)
else disp('not divisable by 5')
end
end
div5_sum = sum(div5)