GLD
GLD

Reputation: 1

Finding even numbers with while as

I'm doing this assignment:

Write a program that prints all even numbers less than the input number using the while loop.

The input format:

The maximum number N that varies from 1 to 200.

The output format:

All even numbers less than N in ascending order. Each number must be on a separate line.

N = int(input())
i = 0
while 200 >= N >= 1:
    i += 1
    if i % 2 == 0 and N > i:
        print(i)

and its output like:

10  # this is my input
2
4
6
8

but there is an error about time exceed.

Upvotes: 0

Views: 1379

Answers (2)

Pablochaches
Pablochaches

Reputation: 1068

The problem is that the while loop never stops

while 200 >= N >= 1 In this case because you never change the value of N the condition will always be true. Maybe you can do something more like this:

N = int(input())
if N > 0 and N <= 200:
    i = 0
    while i < N:
        i += 2
        print(i)
else 
    print("The input can only be a number from 1 to 200")

Upvotes: 0

AksLolCoding
AksLolCoding

Reputation: 157

The simple code would be:

import math

N = int(input(""))
print("1. " + str(N))

num = 1

while num < math.ceil(N/2):
    print (str(num) + ". " + str(num * 2))
    num += 1

Upvotes: 1

Related Questions