0% found this document useful (0 votes)
3 views5 pages

Prime Number Finder in Matlab

The document outlines a MATLAB program called 'Prime Number Finder' designed to check if a number is prime and to find all prime numbers within a specified range. It includes user prompts for input and handles various cases such as invalid inputs and empty results. The program is supervised by Dr. Abdelmoniem Fouda and is authored by John Rafik William from section TH7.

Uploaded by

ricotanicgaming
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Prime Number Finder in Matlab

The document outlines a MATLAB program called 'Prime Number Finder' designed to check if a number is prime and to find all prime numbers within a specified range. It includes user prompts for input and handles various cases such as invalid inputs and empty results. The program is supervised by Dr. Abdelmoniem Fouda and is authored by John Rafik William from section TH7.

Uploaded by

ricotanicgaming
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Matlab Task

Name: John Rafik William


Section: TH7
Supervised By: [Link] Fouda
Prime Number Finder
Objective:
The Prime Number Finder helps users check if a number is prime
and find all prime numbers within a range, making it easy to work
with prime numbers.
Code:
% Welcome message
disp('Prime Number Finder');
disp('-------------------');
disp('Choose an option:');
disp('1. Check if a number is prime');
disp('2. Find all prime numbers in a range');

% Get user choice


choice = input('Enter your choice (1/2): ');

switch choice
case 1 % Check if a number is prime
num = input('Enter a number: ');
if num < 2
disp('A prime number must be greater than
or equal to 2.');
elseif isprime(num)
fprintf('%d is a prime number.\n', num);
else
fprintf('%d is not a prime number.\n',
num);
end

case 2 % Find all prime numbers in a range


range_start = input('Enter the start of the
range: ');
range_end = input('Enter the end of the
range: ');

if range_start > range_end


disp('Invalid range! The start of the
range must be less than or equal to the end.');
else
primes_in_range = [];
for i = range_start:range_end
if isprime(i)
primes_in_range =
[primes_in_range, i];
end
end
if isempty(primes_in_range)
fprintf('No prime numbers found
between %d and %d.\n', range_start, range_end);
else
fprintf('Prime numbers between %d and
%d are:\n', range_start, range_end);
disp(primes_in_range);
end
end

otherwise
disp('Invalid choice! Please restart the
program and select 1 or 2.');
end

You might also like