C program to Inverted pyramid number pattern
- Home
- Number pattern
- C program to Inverted pyramid number pattern
- On
- By
- 0 Comment
- Categories: Number pattern, pyramid triangle
C program to Inverted pyramid number pattern
C program to Inverted pyramid number pattern
In this tutorial, we will learn about C program to Inverted pyramid number pattern
This program displays various different number Pyramid pattern using nested for loop in C programming language
program 1
Code to inverted pyramid pattern 1
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int i,j,rows,space=0; printf("Enter the number of rows"); scanf("%d",&rows);//taking numer of rows from user for(i=rows; i>=1; i--){ for each row //outer for loop for(j=1; j<=space; j++)//print space at the begining printf(" "); for(j=1; j<=i; j++)//print right side of pyramid printf("%d",j); for(j=i-1; j>=1; j--)//print right side of pyramid printf("%d",j); printf("\n"); space++; } getch(); return 0; }
When the above code is executed, it produces the following results:
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 6 5 4 3 2 1 1 2 3 4 5 6 5 4 3 2 1 1 2 3 4 5 4 3 2 1 1 2 3 4 3 2 1 1 2 3 2 1 1 2 1 1
program 2
Code to inverted pyramid pattern 1
Program
#include <stdio.h> #include <stdlib.h> int main() { int i,j,k; int rows,count=1,num; printf("Enter the number of rows you want\n"); scanf("%d",&rows); count=1; num=1+(rows-1)*2; for(i=1; i<=rows; ++i){//outer for loop,(parent) for(j=1; j<=count; ++j){//inner for loop(child) printf(" ");//this loop print space for pyramid } for(k=num; k>=1; --k){//inner for loop(child) printf("%d",i);//this loop print number for pyramid } count++; num=num-2; printf("\n"); } getch(); return 0; }
When the above code is executed, it produces the following results:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 8 8 9
program 3
Code to inverted pyramid pattern 3
#include <stdio.h> #include <stdlib.h> int main() { int i,j,k,rows,counter,temp,num; printf("Enter number of rows you want: \n"); scanf("%d",&rows); printf("Here your pattern\n"); counter=1; temp=1+(rows-1)*2; for(i=1; i<=rows; i++){ num=1; for(j=1; j<=counter; j++) { printf(" ");//print space in beginning } for(k=temp; k>=1; k--){ printf("%d", num);//print number for pattern num++; } counter++; temp=temp-2; printf("\n"); } return 0; }
When the above code is executed, it produces the following results:
Enter number of rows you want: 5 Here your pattern 123456789 1234567 12345 123 1
Suggested for you
Do-while loop in Java language
Similar post
Java program to print star pyramid pattern
C program to print star pyramid pattern
C++ program to print star pyramid pattern
Python program to print star pyramid pattern
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