In: Computer Science
Using Matlab
Once the user select one of the above choices you have to read the value entered by the user and then to call the respective function. If the user entered a number that is not equal to 1 or 2 or 3 or 4, the program should ask the user to re-enter again.
Then, you have to use “input” to enter the values of the variables (inputs of the functions).
For the digits function, you have to enter only positive number. For the minimum function, you have to enter greater than or equal to zero values and for the prime function you have to enter greater than 1 values.
For all of the functions, the program should ask the user to re-enter again if the user entered wrong numbers.
Function Code
=======================================
function [d,p] = digits_function(n)
dd = num2str(n) - '0';
d = length(dd);
p = sum(dd);
end
=======================================
function y = print_Min_function(f,s,th)
a = [f;s;th];
y = min(a);
end
===================================
function y = prime_function(n)
if(isprime(n))
disp('Number is Prime')
fprintf('\n')
y = n;
else
disp('Number is not Prime')
fprintf('\n')
y = n;
end
end
===================================
Script Code
while(1)
disp('To use the digits function you have to enter 1.')
disp('To use the minimum function you have to enter 2.')
disp('To use the prime function you have to enter 3.')
num = input('Enter Your Choice: ');
if(num>4)
continue;
elseif(num==4)
break;
end
fprintf('\n')
if(num==1)
x = input('Enter number: ');
if(x<0)
continue;
end
[number_digits ,sum_digits] = digits_function(x);
fprintf('The number of digits are %.0f\n\n',number_digits)
fprintf('Sum of digits is %.2f\n\n',sum_digits)
elseif(num==2)
a = input('Enter First number: ');
b = input('Enter Second number: ');
c = input('Enter Third number: ');
if(a<0 || b<0 || c<0)
continue;
end
min_number = print_Min_function(a,b,c);
fprintf('Minimum number is %.2f\n\n',min_number)
elseif(num==3)
d = input('Enter number: ');
if(d<2)
continue;
end
result_prime = prime_function(d);
else
end
end
============================================
Results
No output with invalid inputs
Exits when 4 is entered
====================================================