C Program to Print Patterns

373 views

In this example, you will learn how to print patter using C program such as half pyramids of numbers, inverted pyramids numbers, Inverted Half pyramid of Numbers, Hollow Half Pyramid of Numbers, Full Pyramid of Numbers, Hollow Full Pyramid of Numbers, Hollow Inverted Half Pyramid of Numbers in C Programming.

C Examples
Half pyramid of Numbers
Inverted Half pyramid of Numbers
Hollow Half pyramid of Numbers
Full Pyramid of Numbers
Hollow Full Pyramid of Numbers
Hollow Inverted Half Pyramid of Numbers

Half Pyramid of Numbers

1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Code language: C/AL (cal)

C Program:

#include<stdio.h> int main() { int rows; printf("Enter the number of rows:"); scanf("%d",&rows); for(int i=1;i<=rows;i++) { for(int j=1;j<=i;j++) { printf("%d ",j); } printf("\n"); } return 0; }
Code language: PHP (php)

Inverted Half Pyramid of Numbers

1 2 3 4 5 1 2 3 4 1 2 3 1 2 1

C Program:

#include<stdio.h> int main() { int rows; printf("Enter the numbers of rows:"); scanf("%d",&rows); for(int i=rows;i>=1;i--) { for(int j=1;j<=i;j++) { printf("%d ",j); } printf("\n"); } return 0; }
Code language: C/AL (cal)

Hollow Half Pyramid of Numbers

1 1 2 1 3 1 4 1 2 3 4 5

C Program:

#include<stdio.h> int main() { int rows; printf("Enter the number of rows:"); scanf("%d",&rows); for(int i=1;i<=rows;i++) { for(int j=1;j<=i;j++) { if(i==rows) { printf("%d",j); } else { if(j==1||j==i) printf("%d",j); else printf(" "); } } printf("\n"); } return 0; }
Code language: C/AL (cal)

Full Pyramid of Numbers

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

C Program:

#include<stdio.h> int main() { int rows; printf("Enter the number of rows:"); scanf("%d",&rows); for(int i=1;i<=rows;i++) { for(int z=rows;z>i;z--) { printf(" "); } for(int j=i;j<=i*2-1;j++) { printf("%d",j); } for(int z=(i-1)*2;z>=i;z--) { printf("%d",z); } printf("\n"); } return 0; }
Code language: PHP (php)

Hollow Full Pyramid of Numbers

1 1 2 1 3 1 4 1 2 3 4 5

C Program:

#include<stdio.h> int main() { int rows; int z=1,q=1; printf("Enter the number of rows:"); scanf("%d",&rows); for(int i=1;i<=rows;i++) { for(int z=rows;z>i;z--) { printf(" "); } for(int j=1;j<=i*2-1;j++) { if(i==rows) { if (j%2==0) { printf(" "); z++; } else printf("%d",z); } else { if(j==1) { printf("%d",j); } else if(j==i*2-1) { printf("%d",q+1); q++; } else { printf(" "); } } } printf("\n"); } return 0; }
Code language: PHP (php)

Hollow Inverted Half Pyramid of Numbers

1 2 3 4 5 2 5 3 5 4 5 5

C Program:

#include<stdio.h> int main() { int n; printf("Enter the value of N:"); scanf("%d",&n); for(int i=1;i<=n;i++) { for(int j=i;j<=n;j++) { if (i==1) { printf("%d",j); } else { if(j==i||j==n) printf("%d",j); else printf(" "); } } printf("\n"); } return 0; }
Code language: PHP (php)

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Enable Notifications    OK No thanks