C program to pyramid number pattern
- Home
- Number pattern
- C program to pyramid number pattern
- On
- By
- 0 Comment
- Categories: Number pattern, pyramid triangle
C program to pyramid number pattern
C program to pyramid number pattern
In this tutorial, we will learn about C program to pyramid number pattern.
We can display various pyramid pattern using nested for loop in C programming language
pyramid pattern in C language
Code to Pyramid pattern 1
Program
#include <stdio.h> #include <stdlib.h> int main() { int i,j,k=9,m; for(i=1; i<=9; i++){ for(j=1; j<=k; j++){ printf(" "); } for(m=1; m<=i; m++){ printf("%d",i); printf(" "); } printf("\n"); k=k-1; } getch(); return 0; }
When the above code is executed, it produces the following results:
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9
this Pyramid is made of numers
Code to Pyramid pattern 2
Program
#include <stdio.h> #include <stdlib.h> int main() { int rows,i,j,k,l=1; printf("Enter number of rows you want: "); scanf("%d",&rows); printf("Pyramid pattern 1\n"); for(i=1; i<=rows; i++){ for(j=1; j<=rows-i; j++){ printf(" "); } for(k=1; k<=i; k++, l++){ printf("%d ",l); } printf("\n"); } return 0; }
When the above code is executed, it produces the following results:
Enter number of rows you want : 9 Pyramid pattern 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
Code to Pyramid pattern 3
pascal’s triangle piramid pattern
Program
#include <stdio.h> #include <stdlib.h> int main() { int rows,i,j,space_count,count=1; printf("Enter the number of rows you want:"); scanf("%d",&rows); for(i=0; i<=rows; i++){//outer for loop - parent for for(space_count=1; space_count<=rows-i; space_count++ ){ printf(" "); //print space, inner for loop(child) } for(j=0; j<=i; j++) { if(j==0 || i==0) count=1; else count=count*(i-j+1)/j; printf("%4d",count); } printf("\n"); } printf("Pascal's pyramid pattern\n"); getch(); return 0; }
When the above code is executed, it produces the following results:
Enter the number of rows you want:7 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1
Code to Pyramid pattern 4
#include <stdio.h> #include <stdlib.h> int main() { int i,j=0,rows,num=0,count=0,spaceCount; printf("Enter the number of rows you want"); scanf("%d",&rows); for(i=1; i<=rows; ++i){//outer for loop(parent) for(spaceCount=1; spaceCount<=rows-i; spaceCount++){ printf(" ");//print space for Pyramid pattern count++; } while(j!=2*i-1) { if(count<=rows-1) { printf("%d", i+j); num++; } else{ num++; printf("%d",(i+j-2*num)); } ++j; } num=count=j=0; printf("\n"); } return 0; }
When the above code is executed, it produces the following results:
Enter the number of rows you want:5 1 2 3 4 3 4 5 6 7 4 5 6 7 89 10 5 6 7 8 9 10 11 12 13
Code to Pyramid pattern 5
Program
#include <stdio.h> #include <stdlib.h> int main() { int i,j=0,rows,num=0,count=0,spaceCount; printf("Enter the number of rows you want:"); scanf("%d",&rows); for(i=1; i<=rows; ++i){//outer for loop(parent) for(spaceCount=1; spaceCount<=rows-i; spaceCount++){ printf(" ");//print space for Pyramid pattern count++; } while(j!=2*i-1) { if(count<=rows-1) { printf("%d", i+j); ++count; } else{ num++; printf("%d",(i+j-2*num)); } ++j; } num=count=j=0; printf("\n"); } return 0; }
Enter the number of rows you want:5 1 2 3 2 3 4 5 4 3 4 5 6 7 6 5 4 5 6 7 8 9 8 7 6 5
Code to Pyramid pattern 6
Program
#include <stdio.h> #include <stdlib.h> int main() { int i,j,rows; printf("Enter number of rows you want: "); scanf("%d",&rows); printf("Pyramid pattern 1\n"); for (i=1; i<=rows; i++){ for (j=1; j<=rows-i; j++){ printf(" "); }//print space at the beginning of each row for(j=1; j<i; j++) { printf("%d ",j);//print left part of pyramid } printf("\n"); } getch(); return 0; }
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8
Similar post
Floyd’s triangle number pattern using for loop in C
Floyd’s triangle pattern using nested for loop in Java
Floyd’s triangle pattern using nested while loop in Java
Hollow pyramid triangle pattern in C++ language
Rhombus pattern in Java using for loop
Rhombus pattern in C using while loop
Rhombus pattern in C++ using do-while loop
Suggested for you
Nested for loop in Java language
Nested for loop in C++ language
Nested for loop in Python language