Write a MatLab program that will do the following 1 Read a n

Write a MatLab program that will do the following: 1. Read a number from the keyboard. 2. If the number is 0 or less, or non-integer, display an error and ask for that number again, until a positive integer is given. 3. Find all of its prime factors. 4. Output all of these prime factors on the screen, all appearing on a single line, followed by the message ‘End of program.’ 5. Use loops to accomplish your tasks. Do NOT use functions such as factor(), isprime(), primes(), etc. Do NOT use vectors or matrics

Solution

z=input(\'Enter your positive number: \');
while( z < 0 )
fprintf(\'%d Not a valid postive number.Please Try again..!! \ \', z);
z=input(\'Enter your positive number: \');
end
num=z;
str=\'\';
for i = 2 : num
s = 0;
% Finding the factors for a number and checking whether it is prime or not
while num/i == floor(num/i)
num = num/i;
s = s + 1;
end
  
if s > 0
str = [str \' \' num2str(i)];
  
if num == 1
break
end
end

end
fprintf(\'Prime Factors of %d are %s\ \',z,str);
fprintf(\'\ End of program.\');

Write a MatLab program that will do the following: 1. Read a number from the keyboard. 2. If the number is 0 or less, or non-integer, display an error and ask f

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site