I know the answers but i have no idea how to get them. can you please explain st
ID: 3802366 • Letter: I
Question
I know the answers but i have no idea how to get them. can you please explain step by step. thank you!
1. What output occurs for the following program on the given input user str input Enter a positive integer Line 1 my int int (user str) count 0 while my int 0 if my int 2 1: my int my int/2 else my int my int 1 count count 1 Line 2 print (count) Line 3 print (my int) Line 4 (a) Given user input of 11, what value is output by Line 3 of the program? (b Given user input of 12, what value is output by Line 4 of the programExplanation / Answer
Good evening..
To understand the output of the program or the calculation of the value first we need to know the difference between division("/") and modulus("%") operators.
Division operator always results the quotient.for example 10/2=5
whereas modulus operator results remainder.for example 10%2=0
now let me run the program with two inputs:
Firstly my_int =11
Step1:while 11>0 #condition true,we enter into the loop.the loop will run until my_int value becomes zero.
in the if condition we are doing modulus division with 2.
hence 11%2=1 (it gives remainder as the result).
if condition is true then we are asssigning a new value to the my_int with 11/2=5(it gives quotient as answer).
else statement will be ignored.count incremented by 1.
Step2:again check for while condition
now my_int value is 5 again while condition is true and we are in to the loop .
now in the if statement 5%2 =1 hence the if statement also true.so my_int=5/2=2.
else statement will be ignored.count incremented by 1. count=2.
Step3:Now with the new my_int value is 2.again we check the while condition and it is true.now checking the if statement 2%2=0.then the condition false.hence the control moves to else statement where my_int=my_int-1=2-1=1.
count incremented by 1. coumt=3
Step4:Now with the new my int value 1 we are again in to the loop and the if statement also fails then in the else statement my_int value become 0.
finally count becomes 4.
a)if my_int =11 then the count vaalue is 4.
if my_int=12:
step1:while 12 >0 ,condition is true then we are into the loop.ans then check for if condition.12%2=0.hence if condition fails.then in the else my_int becomes 11.
count value will be 1.
now with the my_int value(11) again the above mentioned steps repeated until it becomes 0.
b) hence the Given user input 12 output value by Line 4 is 0.