0% found this document useful (0 votes)
8 views

Practice Problems in Userdefined Function and If Statement SET 2

Uploaded by

raad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Practice Problems in Userdefined Function and If Statement SET 2

Uploaded by

raad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Write a script areamenu that will print a list consisting of “cylinder”,


“circle”, and “rectangle”. It prompts the user to choose one, and then
prompts the user for the appropriate quantities (e.g., the radius of the
circle) and then prints its area.
If the user enters an invalid choice, the script simply prints an error message.
Surface Area of cylinder is 𝟐𝝅𝒓𝒉 + 𝟐𝝅𝒓
Area of circle is 𝝅𝒓𝟐
Area of rectangle is Length *Width

disp('if you want to find surface area of cylinder, enter choice=1');


disp('if you want to find area of Circle, enter choice=2');
disp('if you want to find area of Rectangle, enter choice=3');
choice=input('enter your choice=');
if choice ==1
r=input('enter the radius of cylinter=');
h=input('enter the hight of cylinder=');
A=2*pi*r*h+2*pi*r;
disp(['surface area of cylinder=',num2str(A)]);
elseif choice==2
r=input('enter the radius of circle=');
A=pi*r^2;
disp(['area of circle=',num2str(A)]);
elseif choice==3
Length=input('length of rectangle=');
Width=input('width of rectangle=');
A=Length*Width;
disp(['area of rectancler=',num2str(A)]);
else
disp('invalid choice');
end
2. A telecom company wants to reward the salespersons for selling the company phone
lines based on the following table:
Reward per phone line
Phone lines
Residential Business
0-50 10 SR 15 SR
51-200 15 SR 25 SR
>200 30 SR 40 SR

a) Write a MATLAB user defined function using if statements to compute the total
reward for one salesperson based on two inputs, number of residential phone lines
sold and business phone lines sold.
Answer:

function reward =sales(R,B)


if(R+B)<=50
reward=10*R+15*B;
elseif (R+B)<=200
reward=15*R+25*B;
else
reward=30*R+40*B;
end
end
b) Write a MATLAB program that the user to enter number of residential and business
lines he sold. Use the function defined in part (a) to compute the reward. The
maximum reward for any salesman cannot exceed 8500 SR.

Answer:

R=input('enter R=');
B=input('enter B=');
reward=sales(R,B);

if reward >8500

disp('reward = 8500');
else
disp(['reward= ',num2str(reward)]);
end

(or)

R=input('enter R=');
B=input('enter B=');
reward=sales(R,B);
if R||B<0
disp('invalid number');
elseif reward >8500
disp('reward = 8500');
else
disp(['reward= ',num2str(reward)]);
end

3. The eccentricity of an ellipse is defined as

𝑏 2
√1 − ( )
𝑎

where a is the semimajor axis and b is the semiminor axis of the ellipse.
A script prompts the user for the values of a and b. As division by 0 is not
possible, the script prints an error message if the value of a is 0. If a is not 0, the
script calls a function to calculate and returns the eccentricity, and then the script
prints the result. Write the script and the function.
Answers:
Main program:
a=input(‘enter the value of a’);
b=input(‘enter the value of b’);
if a==0;
disp(‘Error’)
else
E = elp(a,b);
disp(E)
User Defined Function:
function E = elp(a,b)
E=sqrt(1-(b/a)^2);
End

4. a) Write a MATLAB user defined function called leapyear that determine


whether the year is a leap year or not. The output is a variable extra_day,
which should be 1 if the year is a leap year and 0 otherwise. The rules for
determining leap year in the Gregorian calendar are;
All year evenly divisible by 400 or 4 but not by 100 are leap years.
b) Write a MATLAB program that asks the user to enter a year and determines
whether it is a leap year or not , using the function defined in part a,
For example, the years 2000,2010 and 2011 are not leap year, but 2004 and 1996
are leap year.
Answers:
function extra_day=leapyear(year)
if (mod(year,400)==0||mod(year,4)==0)&&mod(year,100)~=0
extra_day=1;
else
extra_day=0;
end
end
b)
year=input('Enter the year=');
extra_day=leapyear(year);
if extra_day==1
fprintf('The year %d is leap year\n',year)
else
fprintf('The year %d is not a leap year\n',year)
end
(or)
year=input('Enter the year=');
extra_day=leapyear(year);
if extra_day==1
disp('The given year is leap year’)

else
disp('The given year is not a leap year’)

end

(or)

year=input('Enter the year=');


extra_day=leapyear(year);
if extra_day==1

disp ( ['The year ',num2str(year),' is leap year'] );

else
disp ( ['The year ',num2str(year),' is not aleap year'] );

end
5. In a script, the user is supposed to enter either a ‘y’ or ‘n’ in response to a
prompt.
The user’s input is read into a character variable called “letter”. The script will
print “OK, continuing” if the user enters either a ‘y’ or ‘Y’,
or it will print “OK, halting” if the user enters a ‘n’ or ‘N’ or “Error” if the user
enters anything else.
Answers:

letter=input ('enter the letter =','s');


if letter=='Y'||letter=='y'
disp('OK, Continuing');
elseif letter=='N'||letter=='n'
disp('OK, Halting');
else
disp('Error');
end

6. Input 2 numbers and perform arithmetic operation based on the operator


given below:
+ - Add
- - Subtract
* - Multiply
/ - Divide
Other than the above, display Invalid Option.
A= input('Enter the value of A=');
B= input('Enter the value of B=');

operator=input('Enter your operator Choice =','s');%


Input Should be declare as string

if operator=='+'
C=A+B;
elseif operator=='-'
C=A-B;
elseif operator=='*'
C=A*B;
elseif operator=='/'
C=A/B;
else
disp('invalid operator')
end
disp(['The value of C=',num2str(C)]);

7. Display the type of the input character as follows:


a,e,i,o,u - Vowel
0-9 - Numeral
Others - Consonant

X=input('Enter the number or letter =','s');% X(input)


should be a string because of mixed combination of
letters and Numbers.

if X=='a'||X=='e'||X=='i'||X=='o'||X=='u'
disp('X is a vowel')
elseif X=='0'||X=='1'||X=='2'||X=='3'||X=='4'||X=='5'||X=='6'
||X=='7'||X=='8'||X=='9'

disp('X is a Number')

else
disp('X is Consonant');
end

You might also like