prime number.there isn't any error. my ans isn't showed up

18 次查看(过去 30 天)
% finding prime number
counter=0;
for j= 1:10;
for i=1:1:j
if mod (j,i)==0
counter=counter+1;
end
end
if counter==2
fprintf('%d is prime', j);
end
end
there isn't any error. my ans isn't showed up

采纳的回答

Bruno Luong
Bruno Luong 2018-11-2
编辑:Bruno Luong 2018-11-2
There is actually one error (where counter is reset)
for j= 1:10
counter=0; % <- reset for every i to be checked
for i=1:1:j
if mod (j,i)==0
counter=counter+1;
end
end
if counter==2
fprintf('%d is prime\n', j);
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Number Theory 的更多信息

标签

尚未输入任何标签。

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by