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

Thursday, May 2, 2013

Nested loops: print pattern - square of stars

Problem: Print the following pattern for a given n (e.g. n=5 below).

*****
*****
*****
*****
*****

Solution in Python:

def print_pattern(n):
    """
    Author: Mayur P Srivastava
    n is number of colmns or rows.
    """

    for i in range(n):

        s = ""

        for j in range(n):

            s += "*"

        print s




Concepts learned: nested loops.

No comments:

Post a Comment