Write a script that will generate seven random integers in the range [-100:200] inclusive and then print the value of each integer and whether it is positive or negative and whether it is even or odd.
An output from this program might be:
-57 is negative and odd.
26 is positive and even.
If zero is one of the random integers generated, remember that zero is even and neither negative or positive.
Hint: even numbers are divisible by 2 with no remainder.
Solve it in MATLAB
Here is your answer, if you have any doubt please comment, i am here to help you. Here is the matlab code for finding the above problem. Here we can use randi() built in method for creating the random numbers. And we can use mod() to find the reminder. So here is the code
%creating 7 random numbers including -100 and 200 , which 1x7 vector. %we can use randi() method to generate random numbers %Uniformly distributed pseudorandom integers r = randi([-100,200],1,7); %iterate through each values in r and check weather it is +ve or -ve and %even or odd for i=1:numel(r) if r(i) <0 %use modulo operator to find the reminder if mod(r(i),2)==0 fprintf(" %d is negative and even\n ",r(i)); else fprintf(" %d is negative and odd\n ",r(i)); end elseif r(i) > 0 if mod(r(i),2)==0 fprintf(" %d is postive and even\n ",r(i)); else fprintf(" %d is positive and odd\n ",r(i)); end else fprintf("zero is even and neither negative or positive"); end end
i put comment in the code, any doubt please comment
Thanks in advance
Get Answers For Free
Most questions answered within 1 hours.