How to output your numbers into an array?

I am taking private tuition for Matlab programming and I am stumped on this -
I wrote a program to find the prime numbers between X and Y. I need to output the prime numbers into an array... but how? Can you edit this program and show me how?
x = input ('Enter start no.: ')
y = input ('Enter end no.: ')
for n = x:y
count = 0;
for b = 1:n
if rem(n,b)== 0
count = count+1;
end
end
if count == 2
display(n);
end
end

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 2월 2일
편집: Azzi Abdelmalek 2013년 2월 2일

0 개 추천

x = input('Enter start no.: ')
y = input('Enter end no.: ')
prim=[];
for n = x:y
count = 0;
for b = 1:n
if rem(n,b)== 0
count = count+1;
end
end
if count == 2
prim(end+1)=n;
end
end
display(prim)
%or
x = input('Enter start no.: ')
y = input('Enter end no.: ')
prim=[];
for n = x:y
if numel(find(~rem(n,2:n)))==1
prim(end+1)=n;
end
end
display(prim)

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by