Home > Mobile >  how to print the n even numbers using a while loop?
how to print the n even numbers using a while loop?

Time:12-19

how to Take an integer n from the user an write a program to print the n even numbers using a while loop.

a=int(input(""))
b=1
while (b>=a):
     c=1*b
     print(c,end=" ")
     b=1

CodePudding user response:

Use a counter to keep track of how many numbers you have printed, so when it reached the desired amount you stop the loop.

Then at each iteration increase the counter by 1 and increase the current_number by 2, so it points to the next even number.

desired_amount = int(input("How many even numbers you want to print? "))

counter = 0
current_number = 0

while counter < desired_amount:
    print(current_number)
    counter  = 1
    current_number  = 2

CodePudding user response:

Take an input, your code is correct

a = int(input(""))

Print even numbers using while Loop? You can do

b = a * 2 # x2 as pointed out by @Sembei
while b != 0: # considering 0 is even number
    if b % 2 == 0: # if it is divisble by 2, it is even
        print(b, end = " ")
    b = b -1 
  • Related