Click here to Skip to main content
15,914,452 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i = 1
while i < 10000 and i > 0 and 1:
print “ Hello ...”
i = 2 * i

What I have tried:

Please help me to solve this as I cannot able to think that the output is 14 times how? Can anyone tell me please!!
Posted
Updated 9-Aug-21 23:34pm

If your code was valid Python - which it isn't ... :laugh:

You are multiplying 1 by 2 each time round the loop:
1
2
4
8
16
...
Which is better represented in binary:
C#
00000001
00000010
00000100
00001000
00010000
...
Because that is how computers store numbers.
And 2^14 is 16384 so your loop ends.

It's more obvious if you do this:
Python
i = 1
while i < 10000 and i > 0 and 1:
  print (' Hello ...')
  print (i)
  i = 2 * i
print('Done!')
print (i)
 
Share this answer
 
Comments
CHill60 10-Aug-21 5:39am    
5'd for the binary representation!
Edit: And I'd love to give you another for "If your code was valid Python "
Just follow the logic ...

At the start i = 1

within the loop (I am assuming it is within the loop - use proper indentation!)
i = 2 * i so

Loop 1 i = 2 * 1 = 2
Loop 2 i = 2 * 2 = 4
Loop 3 i = 2 * 4 = 8

... see the pattern here?

Now do the rest yourself and watch for when i > 10000
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900