Exercise 7: Programming

1. While loops

Write a MATLAB script that uses a while loop to perform the following tasks:


q          Asks the user to enter a number (HINT: Check out the MATLAB function input),


q          Takes that entered-number and divides it into half until the result is less than one, and


q          Counts the number of times the original number is divided into half.


2. FOR Loops

An automobile engine assembly firm wishes to purchase engine casting from a local foundry. The firm requires that the foundry produce engine castings weighing less than 230 kg. The foundry randomly samples 10 engine castings from the production line and finds the castings to weigh: 235, 224, 216, 238, 226, 232, 220, 233, 232, and 227.


Place this data in a single vector. Write a script that uses a for loop to count the number of engine castings that the firm will reject from the above 10 samples.




Rewrite the following MATLAB code. Replace the for and if statement with a single while loop to avoid using the break command.


for n = 2:2:20

                x = 40 + n^2

                if x > 110



                y = sqrt(x)




Write a script that:


q          Asks the user to input a positive integer number,

q          Displays warning message when the input number is not a positive integer,

q          Factors the number ,and

q          Counts the number of each factor number and display them on the screen.

 (HINT: use the MATLAB command factor, primes or diff)



Please enter a postive integer number: 1564

Factor 2 have appeared 2 times.

Factor 17 have appeared 1 times.

Factor 23 have appeared 1 times.