Print first 10 even numbers using a while loop python

Use a while loop with if statement condition i % 2 == 0 then only print the first 10 even numbers in Python.

Simple example code print even numbers of user input value using a while loop in Python. You can use list objects to store value, here we are printing the value using the end keyword.

x = int(input("Enter a number: "))
i = 1

while i <= x:
    if i % 2 == 0:
        print(i, end=" ")
    i = i + 1

Output:

Print first 10 even numbers using a while loop python

Without if statement

num = 2

while num <= 20:
    print(num)
    num = num + 2

Output:

2
4
6
8
10
12
14
16
18
20

Do comment if you have any doubts or suggestions on this Python even number topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Print first 10 even numbers using a while loop python

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.

I am a beginner and I am stuck on this problem, "Write a python code that uses a while loop to print even numbers from 2 through 100. Hint ConsecutiveEven differ by 2."

Here is what I came up with so far:

 while num in range(22,101,2):
              print(num)

asked Oct 17, 2016 at 19:44

Print first 10 even numbers using a while loop python

5

Use either for with range(), or use while and explicitly increment the number. For example:

>>> i = 2
>>> while i <=10: # Using while
...    print(i)
...    i += 2
...
2
4
6
8
10

>>> for i in range(2, 11, 2): # Using for
...    print(i)
...
2
4
6
8
10

answered Oct 17, 2016 at 19:49

Print first 10 even numbers using a while loop python

Moinuddin QuadriMoinuddin Quadri

44.7k12 gold badges92 silver badges118 bronze badges

This is what I'd try:

i=2
while i <= 100:
    if ( i % 2==0):
        print (i, end=', ')
    i+=1

cezar

11.2k6 gold badges42 silver badges81 bronze badges

answered May 31, 2018 at 17:44

1

Your code has several problems:

  • Substituting while for a statement with for syntax. while takes a bool, not an iterable.
  • Using incorrect values for range: you will start at 22.

With minimal changes, this should work:

for num in range(2, 101, 2):
    print(num)

Note that I used 101 for the upper limit of range because it is exclusive. If I put 100 it would stop at 98.

If you need to use a while loop:

n = 2
while n <= 100:
    print (n)
    n += 2

answered Oct 17, 2016 at 19:49

brianpckbrianpck

7,68921 silver badges33 bronze badges

Here is how to use the while loop

 while [condition]:
     logic here

using while in range is incorrect.

num = 0
while num <=100:
    if num % 2 == 0:
        print(num)
    num += 1

answered Oct 17, 2016 at 19:47

MaddyMaddy

1,9704 gold badges24 silver badges57 bronze badges

2

Using a for while write a function that shall display only even numbers from 1 to 20. Make sure your function is called printer

answered Dec 13, 2021 at 8:49

1

How do you print all even numbers in Python while loop?

Example #1: Print all even numbers from given list using for loop Define start and end limit of range. Iterate from start till the range in the list using for loop and check if num % 2 == 0. If the condition satisfies, then only print the number.

How do you print even numbers from 1 to 10 in Python?

Use a while loop with if statement condition i % 2 == 0 then only print the first 10 even numbers in Python.

How do you print the first 10 numbers in Python?

Python: Generate and prints a list of numbers from 1 to 10.
Sample Solution:.
Python Code: nums = range(1,10) print(list(nums)) print(list(map(str, nums))) ... .
Flowchart:.
Python Code Editor: ... .
Have another way to solve this solution? ... .
Previous: Write a Python program to print letters from the English alphabet from a-z and A-Z..

How do you print first and even numbers in Python?

Algorithm to Print First N Even Natural Numbers.
Step 1: Start Program..
Step 2: Read the a number from user and store it in a variable..
Step 3: Find first n even natural number using for loop or while loop..
Step 4: Print first n even natural number..
Step 5: Stop Program..