Using while loop and break statement, write a program on matlab which keeps prom
ID: 3741455 • Letter: U
Question
Using while loop and break statement, write a program on matlab which keeps prompting the user to enter an integer. If the number that the user enters is even, the program responds by printing “right on!” If it is odd, it prints out “odd!”, if the user inputs the number -1, the program finished and exists by printing “bye!”. In order to finish a program (i.e. function) you may use the ‘return’ command. Using while loop and break statement, write a program on matlab which keeps prompting the user to enter an integer. If the number that the user enters is even, the program responds by printing “right on!” If it is odd, it prints out “odd!”, if the user inputs the number -1, the program finished and exists by printing “bye!”. In order to finish a program (i.e. function) you may use the ‘return’ command.Explanation / Answer
while(true)
n = input("Enter an integer:")
if(n==-1)
break;
elseif(mod(n , 2) == 0)
fprintf('right on!')
else (mod(n , 2)!=0)
fprintf("odd!")
end
end
Output:
Enter an integer:n = 4
right on!Enter an integer:n = 5
ans = 1
odd!Enter an integer:n = 6
right on!Enter an integer:n = -1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.