Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- For the pyramid design, try to find a relation between the pattern and the line number.
- For example,
- if you need to print this:
- *
- **
- ***
- ****
- *****
- Then notice that the number of stars on each line is equal to the line number.
- So we need two FOR loops: one to switch the lines and another to print the stars,
- We nest the loops because for each line, we need a loop to count till the line number (for the number of stars)
- and after this job of printing the stars for a particular line completes, we move on to the next line. (After cout-ing a newline character, notice that we don't want this newline character in the inner loop as we don't want a new line after every star, so we put the newline OUTSIDE THE INNER LOOP BUT INSIDE THE OUTER LOOP)
- This continues till we reach the end of the number of lines we want.
- The only thing to remember in nesting loops is, try to find a similarity in the lines (again, here the symmetry is that the number of stars is equal to the line number.)
- */
- #include <iostream>
- using namespace std;
- int main()
- {
- int n;
- cout << "Enter number of lines: ";
- cin >> n;
- for (int i=1; i<=n; i++) // We need to repeat a process n times
- {
- for (int j=1; j<=i; j++) // Print a star i number of times
- {
- cout << "*";
- }
- cout << "\n"; // Move to a new line
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement