Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Learn How to create pyramids in c

107

Published on

Learn How to create pyramids in c

Learn How to create pyramids in c

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
107
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
2
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. How to Create Pyramids in C Learn Creating Pyramids
• 2. 2 lines 3 lines 4 lines
• 3. Analysis- 1.Input – number of lines(n) given by user(4 in the above case) 2.Output – n number of lines and n number of stars in each line(above shape has 4 lines and each line has 4 stars) 4 lines
• 4. Design- 1. One loop is needed for n number of lines 2. And other loop is needed for n stars in each line 3. Printing element * 4. New line after printing all elements in line
• 5. Coding- for(lines=1;lines<=n; lines++) { for(elements=1;elements<=n; elements++) { printf(“*”); } printf(“n”); }
• 6. Coding- for(lines=1;lines<=n; lines++) { for(elements=1;elements<=n; elements++) { printf(“*”); } printf(“n”); } 1 to n means n times(for n lines)
• 7. Coding- for(lines=1;lines<=n; lines++) { for(elements=1;elements<=n; elements++) { printf(“*”); } printf(“n”); } 1 to n means n times(for n stars)
• 8. Coding- for(lines=1;lines<=n; lines++) { for(elements=1;elements<=n; elements++) { printf(“*”); } printf(“n”); } New line after printing all stars in line
• 9. Coding- for(lines=1;lines<=n; lines++) { for(elements=1;elements<=n; elements++) { printf(“*”); } printf(“n”); } Printing element *
• 10. Coding- for(lines=1;lines<=n; lines++) { for(elements=1;elements<=n; elements++) { printf(“*”); } printf(“n”); } 1 to n means n times(for n lines) 1 to n means n times(for n stars) New line after printing all stars in line Printing element *
• 11. Complete Program #include<stdio.h> main() { int n, lines, elements; printf("enter how many lines "); scanf("%d", &n); for(lines=1;lines<=n; lines++) { for(elements=1;elements<=n; elements++) { printf("*"); } printf("n"); } }
• 12. How program works?-1 Suppose n =3 1. lines=1, lines<=n means 1<=3(true) i. elements=1, elements<=n means 1<=3(true) print * elements++ = 2 ii. elements=2, elements<=n means 2<=3(true) print * elements++ = 3 iii. elements=3, elements<=n means 3<=3(true) print * elements++ = 3 iv. elements=4, elements<=n means 4<=3(false) NEW LINE lines++= 2 Output *** _
• 13. How program works?-2 2. lines=2, lines<=n means 2<=3(true) i. elements=1, elements<=n means 1<=3(true) print * elements++ = 2 ii. elements=2, elements<=n means 2<=3(true) print * elements++ = 3 iii. elements=3, elements<=n means 3<=3(true) print * elements++ = 3 iv. elements=4, elements<=n means 4<=3(false) NEW LINE lines++= 3 Output *** _*** _
• 14. How program works?-3 3. lines=3, lines<=n means 3<=3(true) i. elements=1, elements<=n means 1<=3(true) print * elements++ = 2 ii. elements=2, elements<=n means 2<=3(true) print * elements++ = 3 iii. elements=3, elements<=n means 3<=3(true) print * elements++ = 3 iv. elements=4, elements<=n means 4<=3(false) NEW LINE lines++= 4 Output *** _*** _*** _
• 15. How program works?-4 1. lines=3, lines<=n means 4<=3(false) LOOP Finished Output *** _*** _*** _
• 16. Question Q. What is ‘n’ in Coding Section? A. n is the number of lines and number of stars in each line, given by user. Q. Can we execute loop from n to 1? A. Yes, lines and elements both loop can be run from n to 1. We can also run one loop from 1 to n and other from n to 1. It will create no effect on output.
• 17. Run lines loop from n to 1 for(lines=n;lines>=1; lines--) { for(elements=1;elements<=n; elements++) { printf(“*”); } printf(“n”); } It will create no effect on output.
• 18. Run elements loop from n to 1 for(lines=1;lines<=1; lines++) { for(elements=n;elements>=1; elements--) { printf(“*”); } printf(“n”); } It will create no effect on output.
• 19. Run both loops from n to 1 for(lines=n;lines>=1; lines--) { for(elements=n;elements>=1; elements--) { printf(“*”); } printf(“n”); } It will create no effect on output.
• 20. Second Pyramid 4 lines 3 lines 2 lines
• 21. Analysis- 1. Input – number of lines(n) given by user(4 in the above case) 2. Output – a. n number of lines b. Line 1 -> 1 star Line 2 -> 2 stars Line 3 -> 3 stars Line n -> n stars 4 lines
• 22. Design- 1. One loop is needed for n number of lines 2. And other loop is needed for printing elements 3. Number of elements = line number 4. Printing element * 5. New line after printing all elements in line
• 23. Coding- for(lines=1;lines<=n; lines++) { for(elements=1;elements<=lines; elements++) { printf(“*”); } printf(“n”); } New Change
• 24. Complete Program #include<stdio.h> main() { int n, lines, elements; printf("enter how many lines "); scanf("%d", &n); for(lines=1;lines<=n; lines++) { for(elements=1;elements<=lines; elements++) { printf("*"); } printf("n"); } }
• 25. How program works?-1 Suppose n =4 1. lines=1, lines<=n means 1<=3(true) i. elements=1, elements<=lines means 1<=1(true) print * elements++ = 2 ii. elements=2, elements<=lines means 2<=1(false) NEW LINE lines++= 2 Output * _
• 26. How program works?-2 2. lines=2, lines<=n means 2<=3(true) i. elements=1, elements<=lines means 1<=2(true) print * elements++ = 2 ii. elements=2, elements<=lines means 2<=2(true) print * elements++ = 3 iii. elements=3, elements<=lines means 3<=2(false) NEW LINE lines++= 3 Output * _** _
• 27. How program works?-3 3. lines=3, lines<=n means 3<=3(true) i. elements=1, elements<=lines means 1<=3(true) print * elements++ = 2 ii. elements=2, elements<=lines means 2<=3(true) print * elements++ = 3 iii. elements=3, elements<=lines means 3<=3(true) print * elements++ = 4 iv. elements=4, elements<=lines means 4<=3(false) NEW LINE lines++= 4 Output * _** _*** _
• 28. How program works?-4 4. lines=4, lines<=n means 4<=3(false) Output * _** _*** _