Sei sulla pagina 1di 3

ASSIGNMENT NO:5

Q:1 Find greatest of 3 numbers?


Ans:
clear all
close all
clc
a=input('enter the value of a');
b=input('enter the value of b');
c=input('enter the value of c') ;
if a>b&&a>c
a
elseif b>c
b
else
c
end

enter the value of a23


enter the value of b55
enter the value of c6

b=

55

Q:2 Find smallest of 3 numbers?


Ans:
clear all
close all
clc
a=input('enter the value of a');
b=input('enter the value of b');
c=input('enter the value of c') ;
if a<b&&a<c
a
elseif b<c
b
else
c
end

enter the value of a67


enter the value of b56
enter the value of c456

b =

56
Q:3 Input n variables. Find greatest, its index and sort
them.
Ans:
clear all
close all
clc
n=input('enter the no of numbers to be compared');
x=input ('enter n variables');
p=x(1);
k=1;
for i=1:1:n-1 (for finding greatest and its index)
if p<x(i+1)
p=x(i+1);
k=i+1;
end
end
p
k
for k=1:1:n (for sorting)
for j=1:1:n-1
if x(j)<x(j+1)
q=x(j+1);
x(j+1)=x(j);
x(j)=q;
end
end
end
x

enter the no of numbers to be compared6


enter n variables[56 75 45 67 34 89]

p =

89

k =

x =

89 75 67 56 45 34

Q:4 Find smallest of n numbers, index and sort them?


Ans:
clear all
close all
clc
n=input('enter the no of numbers to be compared');
x=input ('enter n variables');
p=x(1);
k=1;
for i=1:1:n-1 (for finding smallest and its index)
if p>x(i+1)
p=x(i+1);
k=i+1;
end
end
p
k
for k=1:1:n (for sorting)
for j=1:1:n-1
if x(j)>x(j+1)
q=x(j+1);
x(j+1)=x(j);
x(j)=q;
end
end
end
x

enter the no of numbers to be compared7


enter n variables[45 67 85 46 57 2 3]

p =

k =

x =

2 3 45 46 57 67 85