Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.

Friday, May 3, 2013

Single loop: Print first n even numbers.


Problem: Print first n even numbers.

Example:

2 4 6 8 10 12 ...

Solution in Python:


def even_numbers(n):
    """
    Author: Mayur P Srivastava
    """


    numbers = []

    for i in range(n):
        numbers.append(2 * (i+1))

    print " ".join([str(e) for e in numbers])


Concepts learned: Single loop.

No comments:

Post a Comment