CBSE Class 12 Computer Science Question 36 of 101

Functions — Question 4

Back to all questions
4
Question

Question 4

Write a function called print_pattern() that takes integer number as argument and prints the following pattern if the input number is 3.

*
**
***

If input is 4, then it should print:

*
**
***
****
Solution
def print_pattern(num):
    for i in range(1, num + 1):
        print("*" * i)
        
num = int(input("Enter a number: "))        
print("Pattern for input", num, ":")
print_pattern(num)
Output
Enter a number: 4
Pattern for input 4 :
*
**
***
****

Enter a number: 5
Pattern for input 5 :
*
**
***
****
*****
Answer