Python Online Compiler
Example: Print a Star Pattern in Python without using loop
C
C++
C#
Java
Python
PHP
main.py
STDIN
Run
# Print a Star Pattern in Python without using loop import math # It's the recursive function # to print the star pattern def starPattern(n = 0, i = 1, r = 1): print (end="*") if (int (math.sqrt(math.pow((i - (2 * n - 1) *(r - 1) - n), 2))) < r) else print (end=" ") if ((i - (2 * n - 1) * (r - 1)) % (2 * n - 1) == 0): print (end="\n") r += 1 if (i < n * (2 * n - 1)): i += 1 starPattern(n, i, r) # It's the driver code for computing print ("-----Enter the size of the pattern-----") # size of the pattern x = int (input ()) # This will print the pattern starPattern(x)
10
Output
Clear
ADVERTISEMENTS