Problem 3 (20 pt): Write a program that asks a user to enter a value between 0 a
ID: 3806909 • Letter: P
Question
Problem 3 (20 pt):
Write a program that asks a user to enter a value between 0 and 10. If the entered value is beyond these bounds, the program asks the user to enter the value again. If the entered value is between 0 and 10, your script divides the value into half again and again, until the value reaches a very small value (0.001 or less).
If the user enters 4, for example, the value becomes 2, 1, 0.5, 0.25, and so forth. Let the program display these intermediate values. The program repeats this division to make the value successively smaller. The program stops calculation once the value reaches 0.001 or less.
Run a case where a user would enter 4. Your script would then respond: "For the entered value of 4, division was repeated 12 times." Of course, your script will not contain such values as 4 and 12, because these values would be determined by the interaction with the user.
Run your script for three different cases: -5, 5, and 15. Submission: 1 m-file and a printout of the command window.
Explanation / Answer
%program
num=input('Enter a number between 0 to 10: ')
while num<0 || num>10
num=input('Re-Enter a number between 0 to 10: ')
end
number=num
count=0
while num>0.001
num=num/2
count=count+1
end
fprintf("For the entered value of %d, division was repeated %d times.",number,count)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.