After each iteration, the value of the number is increased.There are two ways to create loops in Python: with the for-loop and the while-loop.įor loops are used when you have a block of code which you want to repeat a fixed number of times. The inner for loop starts from 1 to i + 1, where i is the row number.The outer for loop iterates from 1 to the total number of rows.number variable is initialized with value 1.Here, i is the row number and j is the value ranging from 0 to i.ħ 8 9 10 Source Code rows = int(input("Enter number of rows: ")) The second inner loop first finds the number to be printed using statement coef = coef * (i - j) // j and then prints it.The first inner loop prints the spaces.The outer loop iterates from 1 to rows + 1.In this example, we have used three for loops. The second inner for loop prints the first half of the pyramid (vertically cut), whereas the last inner for loop prints the other half.ġ 5 10 10 5 1 Source Code rows = int(input("Enter number of rows: ")).The first inner for loop prints the spaces required in each row.The outer for loop iterates from i = rows to i = 1.In this example, we have used a total of 4 for loops. The while loop prints the numbers where 2 * i - 1 gives the number of items in each row.The inner for loop prints the required spaces using formula (rows-i)+1, where rows is the total number of rows and i is the current row number.Here we use two counters count and count1 for printing the spaces and numbers respectively.The outer for loop iterates through each row.Like example 6, this example also makes use of two loops inside a for loop. This formula gives the number of stars for each row, where row is i.ĥ 6 7 8 9 8 7 6 5 Source Code rows = int(input("Enter number of rows: ")) The while loop prints the required number stars using formula 2 * i - 1.Among the two inner loops, the for loop prints the required spaces for each row using formula (rows-i)+1, where rows is the total number of rows and i is the current row number.The outermost loop starts from i = 1 to i = row + 1.This type of pyramid is a bit more complicated than the ones we studied above. * * * * * * * * * Source Code rows = int(input("Enter number of rows: ")) Programs to print full pyramids Example 6: Program to print full pyramid using * The only difference between an upright and an inverted pyramid using numbers is that the first loop starts from the total number of rows to 0. This example is similar to an upright pyramid except that here we start from the total number of rows and in each iteration we decrease the number of rows by 1.Įxample 5: Inverted half pyramid using numbersġ Source Code rows = int(input("Enter number of rows: ")) * Source Code rows = int(input("Enter number of rows: ")) Programs to print inverted half pyramid using * and numbers Example 4: Inverted half pyramid using * Therefore, in each iteration, we increase the value of ascii_value and print its corresponding alphabet. The ascii value for alphabets start from 65 (i.e. The working of the above example is also similar to the other examples discussed above except that the ascii values are printed here. After each iteration of the first loop, we print a new line.Įxample 3: Program to print half pyramid using alphabetsĮ E E E E Source Code rows = int(input("Enter number of rows: ")).In the second loop, we print numbers starting from 1 to j, where j ranges from 0 to i.Once the inner loop ends, we print new line and start printing * in a new line.Įxample 2: Program to print half pyramid a using numbersġ 2 3 4 5 Source Code rows = int(input("Enter number of rows: ")).Similarly, in the 3rd row, we print three *. For example, in the 2nd row, we print two *. Here, the row number gives the number of * required to be printed on that row. In each iteration of this loop, we print i + 1 number of * without a new line. The second loop runs from j = 0 to i + 1.In the first loop, we iterate from i = 0 to i = rows.First, we get the height of the pyramid rows from the user.In the above program, let's see how the pattern is printed. * * * * * Source Code rows = int(input("Enter number of rows: ")) Programs to print triangles using *, numbers and characters Example 1: Program to print half pyramid using * Code to print triangles using *, numbers and charactersĬode to print inverted triangles using * and digits
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |