learn how to create pyramids in c
DESCRIPTION
Learn How to create pyramids in cTRANSCRIPT
![Page 1: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/1.jpg)
How to Create Pyramids in C
Learn Creating Pyramids
![Page 2: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/2.jpg)
2 lines
3 lines
4 lines
![Page 3: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/3.jpg)
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
![Page 4: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/4.jpg)
Design-1. One loop is needed for n
number of lines2. And other loop is needed for n
stars in each line3. Printing element *4. New line after printing all
elements in line
![Page 5: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/5.jpg)
Coding-
for(lines=1;lines<=n; lines++){
for(elements=1;elements<=n; elements++){
printf(“*”);}printf(“\n”);
}
![Page 6: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/6.jpg)
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)
![Page 7: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/7.jpg)
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)
![Page 8: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/8.jpg)
Coding-
for(lines=1;lines<=n; lines++){
for(elements=1;elements<=n; elements++){
printf(“*”);}printf(“\n”);
}
New line after printing all stars in line
![Page 9: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/9.jpg)
Coding-
for(lines=1;lines<=n; lines++){
for(elements=1;elements<=n; elements++){
printf(“*”);}printf(“\n”);
} Printing element *
![Page 10: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/10.jpg)
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 *
![Page 11: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/11.jpg)
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"); }}
![Page 12: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/12.jpg)
How program works?-1Suppose n =31. 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
*** _
![Page 13: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/13.jpg)
How program works?-22. 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
*** _*** _
![Page 14: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/14.jpg)
How program works?-33. 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
*** _*** _*** _
![Page 15: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/15.jpg)
How program works?-41. lines=3, lines<=n means 4<=3(false)
LOOP FinishedOutput *** _*** _*** _
![Page 16: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/16.jpg)
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.
![Page 17: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/17.jpg)
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.
![Page 18: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/18.jpg)
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.
![Page 19: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/19.jpg)
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.
![Page 20: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/20.jpg)
Second Pyramid
4 lines
3 lines
2 lines
![Page 21: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/21.jpg)
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 starLine 2 -> 2 starsLine 3 -> 3 starsLine n -> n stars
4 lines
![Page 22: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/22.jpg)
Design-1. One loop is needed for n number of
lines2. And other loop is needed for
printing elements 3. Number of elements = line number4. Printing element *5. New line after printing all elements
in line
![Page 23: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/23.jpg)
Coding-
for(lines=1;lines<=n; lines++){
for(elements=1;elements<=lines; elements++){
printf(“*”);}printf(“\n”);
}
New Change
![Page 24: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/24.jpg)
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"); }}
![Page 25: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/25.jpg)
How program works?-1Suppose n =41. 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 LINElines++= 2
Output
* _
![Page 26: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/26.jpg)
How program works?-22. 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 LINElines++= 3
Output
* _** _
![Page 27: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/27.jpg)
How program works?-33. 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 LINElines++= 4
Output
* _** _*** _
![Page 28: Learn How to create pyramids in c](https://reader033.vdocuments.us/reader033/viewer/2022052410/54838493b4af9f93068b457b/html5/thumbnails/28.jpg)
How program works?-44. lines=4, lines<=n means 4<=3(false) Output
* _** _*** _