You are on page 1of 18

Chapter 5

Sol

Algorithm
1. Display software use.
2. User enters temperature in Fahrenheit.
3. Make the logic.
4. Display the output.

M file
disp('This software converts from fahrenheit to centigrade and kelvin');

F=input('Enter value in fahrenheit=');

C=(F-32)*5/9;
K=C+273.15;

fprintf('The value in centigrade is %3.3f\n',C);

fprintf('The value in kelvin is %3.3f\n',K);

output
This software converts from fahrenheit to centigrade and kelvin

Enter value in fahrenheit=212


The value in centigrade is 100.000

The value in kelvin is 373.150

This software converts from fahrenheit to centigrade and kelvin

Enter value in fahrenheit=313

The value in centigrade is 156.111

The value in kelvin is 429.261

Sol

Algorithm
1. Display software use
2. User enters the value in feet.
3. Make the logic.
4. Display the output.

M file
disp('This software converts feet to yards and centimeters');

feet=input('Enter value in feet=');

yard=(3*feet);
centimeters= (feet*12*2.54);

fprintf('The value in yards is%3.3f\n',yard);

fprintf('The value in centimeters is%3.3f\n',centimeters);

Output
This software converts feet to yards and centimeters

Enter value in feet=1

The value in yards is 3.000000

The value in centimeters is 30.480000

This software converts feet to yards and centimeters

Enter value in feet=1

The value in yards is3.000

The value in centimeters is30.480

This software converts feet to yards and centimeters

Enter value in feet=12

The value in yards is36.000

The value in centimeters is365.760

Sol

Algorithm
1. Display software use
2. Enter value of radius and angle theta.
3. Make the logic
4. Display the output.

Mfile

disp('This software converts polar to rectangular coordinates');

r=input('Enter the value of radius=');


theta=input('Enter the value of theta in degrees=');

x=r*cosd(theta);

y=r*sin(theta);

fprintf('The value of xcoordinate is %3.3f\n',x);

fprintf('The value of ycoordinate is %3.3f\n',y);

Output

This software converts polar to rectangular coordinates

Enter the value of radius=12

Enter the value of theta in degrees=90

The value of xcoordinate is -0.000

The value of ycoordinate is 10.728

This software converts polar to rectangular coordinates

Enter the value of radius=20

Enter the value of theta in degrees=60

The value of xcoordinate is 10.000

The value of ycoordinate is -6.096

Sol

Algorithm
1. Display software use.
2. Enter value of x and y.
3. Make logic.
4. Display the output.

Mfile

disp('This software converts from rectangular coordinates to polar');

x=input('Enter the value of x=');

y=input('Enter the value of y=');

r=sqrt(x^2+y^2);

theta=atand(y/x);

fprintf('The value of radius is %3.3f\n',r);

fprintf('The value of theta in degrees is %3.3f\n',theta);

Output
This software converts from rectangular coordinates to polar

Enter the value of x=3

Enter the value of y=4

The value of radius is 5.000

The value of theta in degrees is 53.130

Sol
Algorithm

1. Display the software use.


2. User enters the radius and height.
3. Make the logic
4. Display the output.

M file

disp('This software calculates the volume and surface area of hollow


cylinder.');

r=input('Enter the radius=');

h=input('Enter the height=');

volume=pi*r^2*h;

surfacearea=2*pi*r*h;

fprintf('The volume of cylinder is %3.3f\n',volume);

fprintf('The surface area is %3.3f\n',surfacearea);

Output
This software calculates the volume and surface area of hollow cylinder.

Enter the radius=6

Enter the height=5

The volume of cylinder is 565.487

The surface area is 188.496

This software calculates the volume and surface area of hollow cylinder.

Enter the radius=9

Enter the height=9

The volume of cylinder is 2290.221


The surface area is 508.938

Sol

Already done. In chapter 3

Sol

M file
r=input('Enter the radius=');

d=2*r;

fprintf('The radius and diameter is %0.2f and %0.2f\n',r,d);

output
Enter the radius=4

The radius and diameter is 4.00 and 8.00

Enter the radius=5

The radius and diameter are 5.00 and 10.00

Sol

M file
i=input('Enter value in in inches=');

c=i*2.54;

fprintf('The length in in inches and centimeters are %0.2f and %0.2f\n',i,c);

output

Enter value in in inches=1

The length in in inches and centimeters are 1.00 and 2.54

Enter value in in inches=12

The length in in inches and centimeters are 12.00 and 30.48

Sol

Algorithm
Already given in the question.

Mfile
n=input('Enter an integer=');
char=input('Enter an character=','s');
for i=1:n

fprintf('%s\n',char)
end

output
Enter an integer=4

Enter an character=asd

asd

asd

asd

asd

Enter an integer=12

Enter an character=zxc

zxc

zxc

zxc

zxc

zxc

zxc

zxc

zxc

zxc

zxc

zxc

zxc
Sol

Algorithm
Already given in question.

Mfile

n=input('Enter a number=');

for i=1:n

fprintf('It happened %d times\n',n);

end

Output
Enter a number=6
It happened 6 times
It happened 6 times
It happened 6 times
It happened 6 times
It happened 6 times
It happened 6 times
Enter a number=1
It happened 1 times

Sol

Sol

Sol

Sol

Sol

Sol
Sol

Algorithm
1. Display sript use
2. User enter voltage and current
3. Make the logic
4. Display the resistance in ohms.

Mfile
disp('This script calculates the resistance');

e=input('Enter potiential in volts=');

i=input('Enter current in ohms=');

r=(e/i);

fprintf('The resistance is %3.3fin ohms\n',r);

output
This script calculates the resistance

Enter potiential in volts=4

Enter current in ohms=9

The resistance is 0.444in ohms

This script calculates the resistance

Enter potiential in volts=1

Enter current in ohms=8

The resistance is 0.125in ohms

Sol

Algorithm
1. Display script use.
2. User enters potiential and current.
3. Make logic
4. Display the output.

Mfile
disp('This script calculates the power');

e=input('Enter voltage in volts=');

i=input('Enter current in amperes=');

p=e*i;

fprintf('The power in watts is%8.3f\n',p)


Output
This script calculates the power

Enter voltage in volts=12

Enter current in amperes=10

The power in watts is 120.000

This script calculates the power

Enter voltage in volts=5

Enter current in amperes=2

The power in watts is 10.000

Sol

Algorithm
1. Display software use
2. First distance between two points
3. User enters values
4. Make logic
5. Display the distance
6. Second area of triangle using heroes formula
7. User enter values
8. Make logc
9. Display the area.
M file
disp('This software caluclates distance btw two points and area of triangle');

disp(' First distance between two points');

x1=input('Enter first x coordinate=');

x2=input('Enter second x coordinate=');

y1=input('Enter first y coordinate=');

y2=input('Enter second y coordinate=');

d=sqrt((x1-x2)^2+(y1-y2)^2);

fprintf('The distance between two point is%8.3f\n',d);

disp('Second area of atriangle using heroes formula');

a=input('Enter first side a=');

b=input('Enter second side b=');

c=input('Enter third side c=');

s=(a+b+c)/2;

area=sqrt(s*(s-a)*(s-b)*(s-c));

fprintf('The area of a triangle is%8.3f\n',area);

output

This software caluclates distance btw two points and area of triangle

First distance between two points

Enter first x coordinate=12

Enter second x coordinate=24

Enter first y coordinate=019


Enter second y coordinate=24

The distance between two point is 13.000

Second area of atriangle using heroes formula

Enter first side a=12

Enter second side b=13

Enter third side c=24

The area of a triangle is 41.964

You might also like