Answer to Question #186285 in MatLAB for zain ul abdeen

Question #186285

Make a MATLAB function which count on all the prime numbers from 0-1000.

• Sort the numbers in descending order.

• Sum all the even numbers in ascending order from 0-100.  

• Use only While loop.


1
Expert's answer
2021-05-01T14:13:05-0400
function CountPrimes() 
  Counter = 0; % initialize to zero.
  for x = 0:1000 %x is numbers in between 0 and 1000
    y = isprime(x); %y is true if x is prime
    if y  % did we find a prime at this value of x?
      Counter = Counter + 1; % increment the count of primes
    end
  end
    disp(['The total number of primes found was: ',num2str(Counter)])
end


Sum = 0;
Counter = 1;
% Increase counter by *2* (not by 1)
% Until Counter=100
while Counter <= 100
  Sum = Sum + Counter;     % Matlab is case-sensitive
  Counter = Counter + 2;   % Increase the Counter
end
disp(Sum)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS