Pascal’s triangle is a triangular array of the binomial coefficients. To build the pascal triangle, start with “1” at the top, then continue placing members below it in a triangular pattern. Each member is build using sum of above two numbers (except for the edge, which are all 1). To print triangle, the loop statement will be used many times, so nested Loop statements are used to construct triangular pattern.
For loops are used when we need to execute a statement fixed number of times. Nested “for loop” is used in a program to construct pyramid of digits and pascal triangle in C. If one for-loop is inside other for-loop then it is known as nested for-loop.
GET INSTANT HELP FROM EXPERTS!
- Looking for any kind of help on your academic work (essay, assignment, project)?
- Want us to review, proofread or tidy up your work?
- Want a helping hand so that you can focus on the more important tasks?
Hire us as project guide/assistant. Contact us for more information
Pyramid Triangle:
There are different ways to create pyramid.
- Star Pyramid
- String Pyramid
- Number Pyramid
4 for loops are used to construct above pyramid structure.
- One loop controls the number of lines i.e. the depth/height of the pyramid.
- One loop will print the spaces.
- One loop will print the numbers in ascending order.
- One loop will print the numbers in descending order.
Algorithm
1. Start.
2. Declare the variables
3. Accept the number of rows
4. Initialize outer counter to 1, for counting number of lines
5. Check the value of outer counter variable is less than the total number of rows. if yes, go to step 6 else go to step 15
6. Initialize inner counter variable to 1, to display the digits
7. Check if the value of inner counter variable is <= outer counter variable. If yes, go to step 8 else go to step 14
8. Check if the value of inner counter variable is equal to 1. If yes, go to step 9 else go to step 11
9. Assign the value of inner counter variable to another variable, say x
10. Display value of x.
11. Calculate value of x as (x*outer counter-inner counter+1)/ (outer counter - 1)
12. Display value of x
13. Increment inner counter variable and goto step 7
14. Increment outer counter variable and goto step 5
15. Stop
Leave a Reply