0% found this document useful (0 votes)
9 views3 pages

Matlab Menu Program

The document outlines a menu-driven program that allows users to perform various mathematical operations, including checking for prime numbers, displaying right triangles, generating Fibonacci sequences, and computing factorials. Users can select options from a main menu and are prompted for input, with error handling for invalid entries. The program continues to run until the user chooses to quit.
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)
9 views3 pages

Matlab Menu Program

The document outlines a menu-driven program that allows users to perform various mathematical operations, including checking for prime numbers, displaying right triangles, generating Fibonacci sequences, and computing factorials. Users can select options from a main menu and are prompted for input, with error handling for invalid entries. The program continues to run until the user chooses to quit.
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

while true

% Show the main options


disp('====== MAIN MENU ======');
disp('1. Check Prime Number');
disp('2. Display Right Triangle');
disp('3. Generate Fibonacci Sequence');
disp('4. Compute Factorial');
disp('5. Quit');
disp('=======================');
userChoice = input('Select an option (1-5): ', 's');

% Check if the input is a valid choice


if isempty(userChoice) || ~any(userChoice == '12345')
disp('Invalid selection. Please pick between 1 and 5.');
continue;
end

switch userChoice
case '1' % Check for Prime Number
again = true;
while again
number = input('Input an integer greater than 1: ');
if isempty(number) || ~isnumeric(number) || number < 2 || fix(number) ~= number
disp('Error: Input must be a whole number greater than 1.');
else
flag = true;
for k = 2:sqrt(number)
if mod(number,k) == 0
flag = false;
break;
end
end
if flag
fprintf('%d is prime.\n', number);
else
fprintf('%d is not prime.\n', number);
end
end
repeat = input('Do you want to test another? (Y/N): ','s');
if isempty(repeat) || upper(repeat) ~= 'Y'
again = false;
end
end

case '2' % Show Right Triangle


again = true;
while again
triangleSize = input('Triangle size (positive integer): ');
if isempty(triangleSize) || ~isnumeric(triangleSize) || triangleSize < 1 || fix(triangleSize) ~
disp('Input should be a positive whole number.');
else
for row = 1:triangleSize
for col = 1:row
fprintf('%d ', col);
end
fprintf('\n');
end
end
repeat = input('Repeat for another size? (Y/N): ','s');
if isempty(repeat) || upper(repeat) ~= 'Y'
again = false;
end
end

case '3' % Fibonacci Sequence


again = true;
while again
len = input('Enter the length of Fibonacci series: ');
if isempty(len) || ~isnumeric(len) || len < 1 || fix(len) ~= len
disp('Enter a positive integer value.');
else
fibSeq = zeros(1,len);
fibSeq(1) = 1;
if len > 1
fibSeq(2) = 1;
for idx = 3:len
fibSeq(idx) = fibSeq(idx-1) + fibSeq(idx-2);
end
end
fprintf('Fibonacci sequence: ');
fprintf('%d ', fibSeq(1:len));
fprintf('\n');
end
repeat = input('Another Fibonacci sequence? (Y/N): ','s');
if isempty(repeat) || upper(repeat) ~= 'Y'
again = false;
end
end

case '4' % Factorial Calculation


again = true;
while again
n = input('Value for factorial: ');
if isempty(n) || ~isnumeric(n) || n < 0 || fix(n) ~= n
disp('Enter a non-negative whole number.');
else
product = 1;
str = '';
for t = n:-1:1
product = product * t;
if t == 1
str = [str, '1'];
else
str = [str, num2str(t), '*'];
end
end
fprintf('The factorial of %s equals %d.\n', str, product);
end
repeat = input('Try another factorial? (Y/N): ','s');
if isempty(repeat) || upper(repeat) ~= 'Y'
again = false;
end
end

case '5'
disp('Exiting the program. Goodbye!');
break;
end
end

You might also like