You are on page 1of 16

Assignment Number: 3.

Problem Statement:
Write a program that allows a user to enter a string containing a day of the week (“Sunday,”
“Monday,” “Tuesday,” etc.) and uses a switch construct to convert the day to its corresponding
number, where Sunday is considered the first day of the week and Saturday is considered the last
day of the week. Print out the resulting day number. Also, be sure to handle the case of an illegal
day name! (Note: Be sure to use the „s‟ option on function input so that the input is treated as a
string.)

Inputs:
day=Name of the day of the week.

Output:
none

Pseudo Code:
1. The user is prompted to enter the day of the week.
2. If the entered day is Sunday, then it displays “Sunday=1” and likewise for other days,
otherwise it displays “invalid day”.
3. Stop.

Program: days.m
%Script file:days

%Purpose:
%To convert the entered day into corresponding numbers.

%Record of revisions:
%Date Programmer Description of change
%======= ============ =====================
%26/09/15 Akhayika Mohapatra Original code

%Define variables:
%day—Name of the day of the week.
clc;
close all;
clear all;
day=input('Enter the day in lower case:','s')
switch(day)
case 'sunday'
disp('sunday=1')
case 'monday'
disp('monday=2')
case 'tuesday'
disp('tuesday=3')
case 'wednesday'
disp('wednesday=4')
case 'thursday'
disp('thursday=5')
case 'friday'
disp('friday=6')
case 'saturday'
disp('saturday=7')
otherwise
disp('Invalid day.')
end

Test Result:
Enter the day in lower case:sunday

day =

sunday

sunday=1
Assignment Number: 3.2

Problem Statement:
Australia is a great place to live, but it is also a land of high taxes. In 2002, individual citizens
and residents of Australia paid the following income taxes:
Taxable income(in A$) Tax on this income
$0-$6,000 None
$6,001-$20,000 17cents for each $1 over $6,000
$20,001-$50,000 $2,380 plus 30cents for each $1 over $20,000
$50,001-$60,000 $11,380 plus 42cents for each $1 over $50,000
Over $60,000 $15,580 plus 47cenys for each $1 over $60,000

In addition, a flat 1.5% Medicare levy is charged on all income. Write a program to calculate
how much income tax a person will owe based on this information. The program should accept a
total income figure from the user and calculate the income tax, Medicare levy, and total tax
payable by the individual.
Inputs:
income=Total income of the user.

Output:
it=income tax on the total income
medicare= medicare levy
total_tax=total tax payable

Pseudo Code:
1. The user is prompted to enter their total income.
2. if(income<=6000)
it=0;
elseif income>6000&&income<=20000
it=(income-6000)*0.17;
elseif income>20000&&income<=50000
it=2380+(income-20000)*0.3;
elseif income>50000&&income<=60000
it=11380+(income-50000)*0.42;
else
it=15580+(income-60000)*0.47;
3. Medicare levy is assigned the value 5.
medicare<-5
4.The total payable tax is calculated.
total_tax<-medicare+it
5.Stop.
Program:tax.m
%Script file:tax

%Purpose:
%To calculate the income tax, medicare levy and total payable tax of the
%entered total income of the user.

%Record of revisions:
%Date Programmer Description of change
%======= ============ =====================
%26/09/15 Akhayika Mohapatra Original code

%Define variables:
% income=Total income of the user.
% it=income tax on the total income
%medicare= medicare levy
%total_tax=total tax payable
clc;
close all;
clear all;
income=input('Enter your total income.');
if income<=6000
it=0;
elseif income>6000&&income<=20000
it=(income-6000)*0.17;
elseif income>20000&&income<=50000
it=2380+(income-20000)*0.3;
elseif income>50000&&income<=60000
it=11380+(income-50000)*0.42;
else
it=15580+(income-60000)*0.47;
end
medicare=income*0.015;
total_tax=medicare+it;
fprintf('Income tax=$%f\nMedicare levy=$%f\nTotal tax
payable=$%f\n',it,medicare,total_tax)

Test Result:
Enter your total income.50000
Income tax=$11380.000000
Medicare levy=$750.000000
Total tax payable=$12130.000000
Assignment Number: 3.3

Problem Statement:
The electricity accounts of residents in a very small town are calculated as follows:
 If 500 units or fewer are used, the cost is 2 cents per unit.
 If more than 500 but not more than 1000 units are used, the cost is $10 for the first 500
units and 5 cents for every unit in excess of 500.
 If more than 1000 units are used, the cost is $35 for the first 1000 units plus 10 cents for
every unit in excess of 1000.
 A basic service fee of $5 is charged, no matter how much electricity is used.
Write a program that enters the following five consumptions into a vector and display the total
charge for each one: 200, 500, 700, 1000, and 1500.

Inputs:
unit=no. of units of electricity used.
i=index value
cost=cost of the units used.
basic_fee=basic fee of using electricity

Output:
total_charge=total cost of using electricity.

Pseudo Code:
1. The user is prompted to enter their total income.
2. if unit<=500
cost=0.02*unit;
elseif unit>500&unit<=1000
cost=10+(0.05*(unit-500));
else
cost=35+(0.1*(unit-1000));
3. basic_fee=5
4. total_charge=basic_fee+cost
5. Stop.

Program:units.m
%Script file:units

%Purpose:
%To calculate the total charge of using electricity for given number of
%units.
%Record of revisions:
%Date Programmer Description of change
%======= ============ =====================
%26/09/15 Akhayika Mohapatra Original code

%Define variables:
% unit=no. of units of electricity used.
%i=index value
%cost=cost of the units used.
%basic_fee=basic fee of using electricity
% total_charge=total cost of using electricity
clc;
close all;
clear all;
unit=[200 500 700 1000 1500]
for i=1:5
if unit(i)<=500
cost=0.02*unit(i);
elseif unit(i)>500&unit(i)<=1000
cost=10+(0.05*(unit(i)-500));
else
cost=35+(0.1*(unit(i)-1000));
end
basic_fee=5;
total_charge=cost+basic_fee;
fprintf('The total charge is %f\n',total_charge)
end

Test Result:
unit =

200 500 700 1000 1500

The total charge is 9.000000


The total charge is 15.000000
The total charge is 25.000000
The total charge is 40.000000
The total charge is 90.000000
Assignment Number: 3.4

Problem Statement:
Write a MATLAB code to add, subtract, multiply and divide two complex numbers depending
on the user‟s choice (e.g. 1-ADDITION, 2SUBTRACTION, 3-MULTIPLICATION, 4-
DIVISION). Prompt the user to input the real and imaginary part of the two complex numbers
separately.

Inputs:
real1=real part of first number.
complex1=imaginary part of first number.
real2=real part of second number.
complex2=imaginary part of second number.
x=first complex number formed using complex function.
y=second complex number formed using complex function.
choice=choice of operation.

Output:
z=result of the operations performed.

Pseudo Code:
1. The user is prompted to enter the real and imaginary part of both the complex numbers.
2. The two complex numbers are formed.
3. The user is prompted to select his choice of operation from the given options.
4. If choice is 1, addition is done, else if the choice is 2, subtraction is done, else if the
choice is 3, multiplication is done, else if the choice is 4, division is done otherwise it
displays “invalid operator”.
5. Stop.

Program:complex_numbers.m
%Script file:complex_numbers

%Purpose:
%To do all basic arithmetic operations on two complex numbers.

%Record of revisions:
%Date Programmer Description of change
%======= ============ =====================
%26/09/15 Akhayika Mohapatra Original code

%Define variables:
% real1=real part of first number.
%complex1=imaginary part of first number.
%real2=real part of second number.
%complex2=imaginary part of second number.
%x=first complex number formed using complex function.
%y=second complex number formed using complex function.
%choice=choice of operation.
% z=result of the operations performed.
clc;
clear all;
close all;
real1=input('Enter the real part of no1.');
complex1=input('enter the complex of no1.');
real2=input('Enter the real part of no2.');
complex2=input('enter the complex of no2.');
x=complex(real1,complex1);
y=complex(real2,complex2);
choice=input('Enter operation:\n1-ADDITION\n2-SUBTRACTION\n3-
MULTIPLICATION\n4-DIVISION.');
switch(choice)
case 1
z=x+y;
disp('sum=')
case 2
z=x-y;
disp('difference=')
case 3
z=x*y;
disp('product=')
case 4
z=x/y;
disp('quotient=')
otherwise
disp('Invalid operator.')
end
disp(z)

Test Result:
Enter the real part of no1.1
enter the complex of no1.3
Enter the real part of no2.5
enter the complex of no2.6
Enter operation:
1-ADDITION
2-SUBTRACTION
3-MULTIPLICATION
4-DIVISION.4
quotient=
0.3770 + 0.1475i
Assignment Number: 3.5

Problem Statement:
The tangent is defined as tan θ=sin θ /cos θ. This expression can be evaluated to solve for the tangent as
long as the magnitude of cos θ is not too near to zero. (if cos θ is zero, evaluating the equation of tan θ
will produce non-numerical value Inf) Assume that θ is given in degrees and write the MATLAB
statements to evaluate tan θ as long as the magnitude of cos θ is greater than or equal to . If the
magnitude of cos θ is less than , write out an error message instead.

Inputs:
theta=angle in radian.
theta1=angle in degrees.
x=sin(theta1)
y=cos(theta1)

Output:
tanTheta=tan(theta1)

Pseudo Code:
1. The user is prompted to enter the angle in radian.
2. The angle is converted into degrees.
3. x and y are found out.
4. if y>=10^-20
tanTheta=x/y;
Print “tanTheta”
else
display “error”
5. Stop.

Program:tan_theta.m
%Script file:tan_theta

%Purpose:
%To find the tangent of the number in degrees based on the conditions given in
%the question.

%Record of revisions:
%Date Programmer Description of change
%======= ============ =====================
%26/09/15 Akhayika Mohapatra Original code

%Define variables:
%theta=angle in radian.
%theta1=angle in degrees.
%x=sin(theta1)
%y=cos(theta1)
% tanTheta=tan(theta1)
clc;
close all;
clear all;
theta=input('Enter the value of theta in radian.')
theta1=theta*(180/pi)
x=sin(theta1)
y=cos(theta1)
if y>=10^-20
tanTheta=x/y;
fprintf('tan(theta)=%f\n',tanTheta)
else
disp('Error')
end

Test Result:
Enter the value of theta in radian.5

theta =

theta1 =

286.4789

x=

-0.5597

y=

-0.8287

Error
Assignment Number: 3.6

Problem Statement:
The cost of sending a package by an express delivery service is $15 for the first two pounds, and
$5 for each pound or fraction thereof over two pounds. If the package weighs more than seventy
pounds, a $15 excess weight surcharge is added to the cost. No package over 100 pounds will be
accepted. Write a program that accepts the weight of a package in pounds and compute the cost
of mailing the package. Be sure to handle the case of overweight packages

Inputs:
w=weight of package.

Output:
p=cost of package.

Pseudo Code:
1. The user is prompted to enter the weight of the package.
2. p=15
3. if w>100
p=0;
display 'The package will not be accepted.'
elseif w>2&&w<=70
p=p+(w-2)*5;
elseif w>70
p=p+(w-2)*5+15;
4. Stop.

Program:package.m
%Script file:package

%Purpose:
%To find the cost of mailing a package according to its weight.

%Record of revisions:
%Date Programmer Description of change
%======= ============ =====================
%26/09/15 Akhayika Mohapatra Original code

%Define variables:
%w=weight of the package.
%p=cost of the package.
clc;
close all;
clear all;
w=input('Enter the weight of the package to be delivered.')
p=15;
if w>100
p=0;
disp('The package will not be accepted.')
elseif w>2&&w<=70
p=p+(w-2)*5;
elseif w>70
p=p+(w-2)*5+15;
end
fprintf('The cost of the package is %f.\n',p)

Test Result:
Enter the weight of the package to be delivered.50

w=

50

The cost of the package is 255.000000.


Assignment Number: 3.7

Problem Statement:
Write a MATLAB program to evaluate the function
Y(x)=ln(1/(1-x))
for any user specified value of „x‟, where x is a number less than one. (Note that ln is the natural
logarithm). Use an if structure to verify that the value passed to the program is legal. If the value of x is
legal, calculate y(x). If not write a suitable error message.

Inputs:
x=the value of the number.

Output:
y=the output of the function.

Pseudo Code:
1. The user is prompted to enter the value of the number.
2. if x>=1.0
display 'The entered value is not legal.'
else
y=log(1/(1-x));
Print the value of y.
3. Stop.

Program:log_x.m
%Script file:log_x

%Purpose:
%To calculate the log of the entered value using the given function.

%Record of revisions:
%Date Programmer Description of change
%======= ============ =====================
%26/09/15 Akhayika Mohapatra Original code

%Define variables:
%x=value of the number.
%y=output of the function.
clc;
close all;
clear all;
x=input('Enter the value of x.')
if x>=1.0
disp('The entered value is not legal.')
else
y=log(1/(1-x));
fprintf('y=%f\n',y)
end

Test Result:
Enter the value of x.0.4

x=

0.4000

y=0.510826
Assignment Number: 3.8

Problem Statement:
Write a MATLAB program to prompt the user to input distance in meter and convert it to
centimeter, millimeter, feet and inch using “switch” structure

Inputs:
len=length in centimetre.
choice=the choice of units.

Output:
val=converted value of the entered length.

Pseudo Code:
1. The user is prompted to enter the length in cm and his choice of unit to which it is to be
converted.
2. If the choice is “inch” then val=len/2.54,else if the choice is “feet” then
val=len/(12*2.54),else if the choice is “meter” then val=len/100, else if the choice is
“centimeter” then val=len otherwise it displays “error”.
3. Stop.

Program:length.m
%Script file:length

%Purpose:
%To convert the entered length in cm into other units-inch, feet, meter,
%centimeter.

%Record of revisions:
%Date Programmer Description of change
%======= ============ =====================
%26/09/15 Akhayika Mohapatra Original code

%Define variables:
% len=length in centimetre.
%choice=the choice of units.
%val=converted value of the entered length.
clc;
close all;
clear all;
len=input('Enter the length in cm:');
choice=input('Enter your choice of unit(inch,feet,meter and
centimeter):','s');
switch(choice)
case 'inch'
val=len/2.54;
case 'feet'
val=len/(12*2.54);
case 'metre'
val=len/100;
case 'centimetre'
val=len;
otherwise
disp('error')
end
fprintf('The convert value is %2.2f and the unit is %s.\n',val,choice)

Test Result:
Enter the length in cm:5
Enter your choice of unit(inch,feet,meter and centimeter):inch
The convert value is 1.97 and the unit is inch.

You might also like