X (1 - 1 2 3) Max X (1) Min X (1) For I 1:length (X) If X (I) Max Max X (I) End End Disp (Max) Disp (Min)

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

1- calculate the maximum and minimum value in vector x

x=[ 1 -1 2 3];

max=x(1);

min=x(1);

for i=1:length(x)

if x(i) < min

min=x(i);

end

if x(i) > max

max=x(i);

end

end

disp(max)

disp(min)
2- Write a Matlab program that have two input integers x and y. It will
output the absolute difference of the two integers.

x=input(‘enter x’);

y=input(enter y’);

Result=0;

if x<y

Result = y-x;

elseif x>y

Result = x-y;

else

Result=0;

end

disp(Result)
3-Determine and print the number of zeros in matrix x

x=[0 1 -1 0 2 0 3];

count = 0;

for i=1: length(x)

if x(i)==0

count=count+1;

end

end

disp(count);

4- Verify that all matrix X element are positive values.

x=[ 1 -1 2 3];

for i=1:length(x)

if x(i)<0

disp('Matrix has negative element');

end

end
5-Print a sequence of asterisk characters in the following configuration

**

***

****

*****

******

*******

********

for i=1: 8

for j=1:i

fprintf ('*');

end

fprintf ('\n');

end
6- Using a while loop to ask the user to input a number 
between 1 and 10 

value = input('Enter a Number between 1 and 10 : ');

while ( value < 1 || value > 10)

disp('Incorrect input.\n');

value = input('Enter a Number between 1 and 10 : ');

end

disp('Correct')
7- Write a script file that asks the User to enter his Math. Exam result and
then display his grad according to the following scheme:

Excellent >= 85 , V.Good >= 75 , Good >= 65 , Pass >= 50 , Fail

d = input(‘Enter your Math. Degree: );

if d >= 85

disp(‘Excellent’);

elseif d >= 75

disp(‘V.Good’);

elseif d >= 65

disp(‘Good’);

elseif d >= 50

disp(‘Pass’);

else

disp(‘Fail);

end
8- Write a script file with a for loop that will iterate from 0 to 20. For each
iteration, it will check if the current number is even or odd, and report
that to the screen (e.g. "2 is even").

for i=0:20

if rem(i,2)==0

disp(‘ number is even’);

else

disp(‘ number is odd’);

end

You might also like