FIRST
With no space in between the stars:Let's go through the code step by step:
The
space()
function takes two parameters,n
andi
, which represent the total number of spaces (n
) and the current row number (i
).Inside the
space()
function, the variablec
is calculated as the difference betweenn
andi
, representing the number of spaces needed in the current row.If
c
is greater than 0, a space character is printedc
number of times using the" "*c
expression. Theend=""
parameter is used to avoid printing a newline character.The
star()
function takes thei
parameter, representing the current row number.If
i
is greater than 0, the expression"*"
is multiplied by(2*i-1)
, which gives the number of stars in the current row. This ensures that the number of stars follows the pattern of an equilateral triangle.Inside the
start()
function, the number of rowsn
is obtained from user input usinginput()
andint()
functions.The variable
b
is assigned the value ofn
to store the initial number of spaces needed.The variable
i
is initialized to 1, representing the current row number.The
while
loop executes as long asn
is greater than 0, indicating there are more rows to print.Inside the loop, the
space()
function is called withb
andi
as arguments to print the required number of spaces before the stars in each row.The
star()
function is called withi
as an argument to print the stars in each row.After printing the stars, the
space()
function is called again withb
andi
as arguments to print the spaces after the stars.A newline character is printed using
print()
to move to the next row.The variables
i
andn
are incremented and decremented, respectively, to move to the next row.The
while
loop continues until all rows are printed.
The changes made are:
- In the
space
function, an extra space character is added after printing the spaces to achieve the desired spacing between the spaces and the stars. - In the
star
function, the "*" is followed by a space character to achieve the desired spacing between the stars.
Comments
Post a Comment