FIRST
With no space in between the stars:Let's go through the code step by step:
The
space()function takes two parameters,nandi, which represent the total number of spaces (n) and the current row number (i).Inside the
space()function, the variablecis calculated as the difference betweennandi, representing the number of spaces needed in the current row.If
cis greater than 0, a space character is printedcnumber of times using the" "*cexpression. Theend=""parameter is used to avoid printing a newline character.The
star()function takes theiparameter, representing the current row number.If
iis 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 rowsnis obtained from user input usinginput()andint()functions.The variable
bis assigned the value ofnto store the initial number of spaces needed.The variable
iis initialized to 1, representing the current row number.The
whileloop executes as long asnis greater than 0, indicating there are more rows to print.Inside the loop, the
space()function is called withbandias arguments to print the required number of spaces before the stars in each row.The
star()function is called withias an argument to print the stars in each row.After printing the stars, the
space()function is called again withbandias arguments to print the spaces after the stars.A newline character is printed using
print()to move to the next row.The variables
iandnare incremented and decremented, respectively, to move to the next row.The
whileloop continues until all rows are printed.
The changes made are:
- In the
spacefunction, an extra space character is added after printing the spaces to achieve the desired spacing between the spaces and the stars. - In the
starfunction, the "*" is followed by a space character to achieve the desired spacing between the stars.
Comments
Post a Comment