0

FOR THE FIRST CODE:

num = 1

while num<=100:
    if num%3==0 or num%5==0:
        continue

    print (num, end=" ")
    num+=1

OUTPUT: 1 2

FOR THE SECOND CODE:

for num in range(1, 101):
    if num%3==0 or num%5==0:
        continue

    print (num, end=" ")

OUTPUT:

1 2 4 7 8 11 13 14 16 17 19 22 23 26 28 29 31 32 34 37 38 41 43 44 46 47 49 52 53 
56 58 59 61 62 64 67 68 71 73 74 76 77 79 82 83 86 88 89 91 92 94 97 98
4
  • 3
    Your first code is infinite. I'm not sure how you got 1 2 as output. Commented Feb 7, 2020 at 17:51
  • Invert condition in while loop and put result printing inside. You skip num increment Commented Feb 7, 2020 at 17:51
  • What @Austin said. In your if condition for the first statement you don't increment your counter before you continue so num%3 is always true and likely freezes your program after that. Commented Feb 7, 2020 at 17:53
  • @Austin It only continues at a multiple of 3 or 5, which 1 and 2 are not. Commented Feb 7, 2020 at 18:20

5 Answers 5

1

You need to add incrementation before you continue

num = 1
while num<=100:
    if num%3==0 or num%5==0:
        num += 1
        continue
    print (num)
    num+=1
Sign up to request clarification or add additional context in comments.

Comments

1

You need to edit your while code in order to achieve same result. In your while loop if num%3 == 0 or num%==5, then the program not executing num += 1, so your program doesn't increment 1. You need to change like this:

num=0
while num <= 100:
    num+=1
    if num%3==0 or num%5==0:
        continue

    print (num, end=" ")

Comments

0

Your code does not call increment 1 in the while loop inside the if statement, it never gets past 3.

Comments

0

It's an infinite loop. Your program never terminates after it reaches num == 3. It goes to the if statement and the continue takes it back to the while check.

Comments

0

Use below logic

        num = 1

        while num <= 100:
            if num % 3 == 0 or num % 5 == 0:
                num += 1
                continue
            print(num, end=" ")
            num += 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.