Updated March 28, 2023
Introduction to Number Patterns in C
Practicing Pattern exercises are always prescribed by many programmers as well as in books as it increases the ability to build logic while using Flow Control Statements. It also enhances logical thinking capabilities. In this article, We are going to see a list of Number patterns to practice for beginners and intermediate programmers.
Examples of Number Patterns in C language
Let us discuss some examples to understand the concept of number patterns in C easily.
Example #1
In the following C program, the user can enter a number of rows to print the number pyramid pattern as he wishes, then the result will be displayed on the screen:
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int n, i, j;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(i = 1; i <= n; i++)
{
for(j = n; j > i; j--)
{
printf(" ");
}
for(j = 1; j <= i; j++)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}
Output:
Example #2
In the following C program, the user can enter the number of rows to print the half pyramid of numbers as he wishes, then the result will be displayed on the screen.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int n, i, j;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(i = 1; i <= n; i++)
{
for(j = 1; j <= i; j++)
{
printf("%d",j);
}
printf("\n");
}
return 0;
}
Output:
Example #3
In the following C program, the user can enter the number of rows to print the half pyramid of numbers as he wishes, then the result will be displayed on the screen.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int n, i, j;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(i = 1; i <= n; i++)
{
for(j = 1; j <= i; j++)
{
printf("%d",i);
}
printf("\n");
}
return 0;
}
Output:
Example #4
In the following C program, the user can enter the number of rows to print the Diamond pattern of numbers as he wishes, then the result will be displayed on the screen.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int n, i, j, k;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(i = 1; i <= n; i++)
{
for(j = i; j <n; j++)
{
printf(" ");
}
for(k = 1; k < (i*2); k++)
{
printf("%d",k);
}
printf("\n");
}
for(i = 4; i >= 1; i--)
{
for(j = n; j > i; j--)
{
printf(" ");
}
for(k = 1; k < (i*2); k++)
{
printf("%d",k);
}
printf("\n");
}
return 0;
}
Output:
Example #5
In the following C program, the user can enter a number of rows to print the inverted half pyramid of numbers as he wishes, then the result will be displayed on the screen.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int n, i, j;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(i = n; i >= 1; i--)
{
for(j = 1; j <= i; j++)
{
printf("%d",j);
}
printf("\n");
}
return 0;
}
Output:
Example #6
In the following C program, the user can enter the number of rows to print the triangular number pattern as he wishes, then the result will be displayed on the screen:
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int n, i, j, k;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(i =1; i <= n; i++)
{
for(j =1; j <= n; j++)
{
if(j <= i)
printf("%d",j);
else
printf(" ");
}
for(j = n; j >= 1;j--)
{
if(j <= i)
printf("%d",j);
else
printf(" ");
}
printf("\n");
}
return 0;
}
Output:
Logic for the above program:
Between these two patterns spaces are printed in decreasing order. There are 10 spaces in 1st row whereas 8 spaces in 2nd row and so on the last row contains 0 spaces.
Example #7
In the following C program, the user can enter number of rows to print the number pyramid pattern as he wishes, then the result will be displayed on the screen:
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i, s, n, j = 0, c = 0, c1 = 0;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(i = 1; i <= n; ++i)
{
for(s = 1; s <= n-i; ++s)
{
printf(" ");
++c;
}
while(j != 2 * i - 1)
{
if (c <= n - 1)
{
printf("%d ", i + j);
++c;
}
else
{
++c1;
printf("%d ", (i + j - 2 * c1));
}
++j;
}
c1 = c = j = 0;
printf("\n");
}
return 0;
}
Output:
Example #8
In the following C program, the user can enter number of rows to print the number pyramid pattern as he wishes, then the result will be displayed on the screen:
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int n, i, j, c = 1;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(i = 1; i <= n; i++)
{
for(j = 1; j <= i; ++j)
{
printf("%d ", c);
++c;
}
printf("\n");
}
return 0;
}
Output:
Example #9
In the following C program, the user can enter number of rows to print the Cross pattern of numbers as he wishes, then the result will be displayed on the screen:
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int n, i, j, c = 1;
int m[5][5] = {0};
printf("Enter the number of rows: ");
scanf("%d",&n);
for(i = 1; i <= 5; i++)
{
for(j = 1; j <= 5; j++)
if(j == i || 6-i == j)
m[i-1][j-1] = c;
if(i < 4) ++c;
else --c;
}
for(i = 0; i < 5; i++)
{
for(j = 0; j < 5; j++)
{
if(m[i][j] == 0)
printf(" ");
else
printf("%d",m[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Example #10
In the following C program, the user can enter number of rows to print the Cross pattern of numbers as he wishes, then the result will be displayed on the screen:
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int n, i, j, c = 1;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(i = 1; i <= (2 * n) - 1; i++)
{
for (j = 1; j <= (2 * n) - 1; j++ )
{
if (i == j || i + j == 2 * n)
printf("%d", c);
else
printf(" ") ;
}
if (i < n)
c++ ;
else
c-- ;
printf("\n") ;
}
return 0;
}
Output:
Example #11
In the following C program, the user can enter number of rows to print the vertical triangle of numbers as he wishes, then the result will be displayed on the screen:
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int n, i, j;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(int i = 1; i < n; i++)
{
for(int j = 1; j <= i; j++)
printf("%d",j);
printf("\n");
}
for(int i = n; i >= 0; i--)
{
for(int j = 1; j <= i; j++)
printf("%d",j);
printf("\n");
}
return 0;
}
Output:
Example #12
In the following C program, the user can enter a number of rows to print the vertical triangular of numbers as he wishes, then the result will be displayed on the screen:
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int n, i, j;
printf("Enter the number of rows: ");
scanf("%d",&n);
for (int i = n; i >= 0; i--)
{
for (int j = 1; j <= i; j++)
printf("%d",j);
printf("\n");
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= i; j++)
printf("%d",j);
printf("\n");
}
return 0;
}
Output:
Example #13
In the following C program, the user can enter the number of rows to print the Half Triangle pattern of numbers as he wishes, then the result will be displayed on the screen:
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int n, i, j, x, y;
printf("Enter the number of rows: ");
scanf("%d",&n);
for (i = 1; i <= n; i++)
{
if (i % 2 == 0)
{
x = 1;
y = 0;
}
else
{
x = 0;
y = 1;
}
for (j = 1; j <= i; j++)
if (j % 2 == 0)
printf("%d",x);
else
printf("%d",y);
printf("\n");
}
return 0;
}
Output:
Example #14
In the following C program, the user can enter the number of rows to print the inverted half pyramid pattern of numbers as he wishes, then the result will be displayed on the screen:
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int n, i, j;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(i = n; i >= 1; i--)
{
for(j = i; j >= 1; j--)
{
printf("%d", i);
}
printf("\n");
}
return 0;
}
Output:
Recommended Articles
We hope that this EDUCBA information on “Number Patterns in C” was beneficial to you. You can view EDUCBA’s recommended articles for more information.