0
485views
Write a python program to print star in a Rectangular pattern
1 Answer
0
3views

Explanation:-

  • To print the rectangle take input of no of rows and columns from the user and store the values in the variables r and c respectively.
  • Initialize 2 for loops to iterate over rows and columns respectively.
  • To print rectangle we have to put up a if condition to print star if it is a 1st row or column or last row or column else print space.

Code:-

r=int(input("No of Rows in rectangle "))
c=int(input("No of Columns in rectangle "))
#Iterate over the rows
for i in range(r):
#Iterate over the columns
    for j in range(c):
#print star if row no is 0 or no of rows-1
#print star if column no is 0 or no of column-1
        if(i==0 or i==(r-1) or
        j==0 or j==(c-1)):
            print("*",end=" ")
#else print space
        else:
            print(" ",end=" ")
    print()

Output:-

enter image description here

Please log in to add an answer.