Python – program to print first n even natural numbers

Python – program to print first n even natural numbers

In this post, we will see how to print first n even natural numbers in python?

Even Numbers:

Any integer that is positive or negative which can be divided exactly by 2 is an even number. The numbers having the last digit is 0, 2, 4, 6 or 8 are even numbers.

Note : Natural number is positive integers. It’s range from 1 to infinity.

Examples:

Input:      n: 5

Output:   5 first even numbers are: 2 4 6 8 10

Input:    n: 6

Output:  6 first even numbers are: 2   4   6   8   10   12

Let’s see the different version of codes for this program :

Code version 1: Using a counter.

In this code, we take a counter variable ‘even’ initialized by 2 and on every iteration, this counter value is incremented by 2.

# main code
if __name__ == "__main__" :

    # input value of n
    n = 11

    # take a even variable
    # and assign 2 to it
    even = 2
    print("even numbers are: ")
    # loop run from 0
    # to n - 1
    for i in range(n) :

        # print value of even variable
        # and end parameter is ','
        # so print all outputs
        # in a single line with ','
        print(even,end = ",")

        # increment by 2
        # in every iteration
        even += 2
Output: even numbers are: 

2,4,6,8,10,12,14,16,18,20,22,

Code version 2: Using an if-else statement along with for loop.

In this code, we run a loop from 2 to 2 * n, and in every iteration, we check the value of i is divisible by 2 or not if divisible then it is even otherwise odd.

# main code
if __name__ == "__main__" :

    # input value of n
    n = 7
    
    print("even numbers are: ")
    # loop run from 2
    # to 2 * n 
    for i in range(2, 2 * n + 1) :

        # if condition is True 
        # that means i % 2 gives 
        # non-zero value then 
        # just ignore this block
        # else print i
        if i % 2 :
            pass

        # if condition is false 
        # that means i % 2 return 0        
        else :
            
            # print value of i variable
            # and end parameter is ','
            # so print all outputs
            # in a single line with ','
            print(i, end = ",")
Output: even numbers are: 

2,4,6,8,10,12,14,

Note : Code version 1 and 2 can also be written using While loop.

Code version 3: Using a range() function concept.

As we know, we can pass 3 parameters in range() function, the third parameter is stepped i.e jump after each iteration.

# main code
if __name__ == "__main__" :

    # input value of n
    n = 7

    print("even numbers are: ")
    # loop run from 2
    # to 2 * n 
    for even in range(2, 2 * n + 1, 2) :

        # print value of even variable
        # and end parameter is ','
        # so print all outputs
        # in a single line with ','
        print(even, end = ",")
Output: even numbers are: 

2,4,6,8,10,12,14,

Leave a Reply

Your email address will not be published.