EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Number Patterns in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Number Patterns in C

Number Patterns in C

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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:

Pyramid in C

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;
}

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,617 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Output:

Half Pyramid of Numbers

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:

Number Patters in C Example 3

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:

Diamond Pattern of Number

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:

Inverted half pyramid of numbers

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:

triangular pattern

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:

Pyramid Pattern Example 7

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:

Half Pyramid Pattern of Numbers

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:

Cross Pattern of Numbers

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:

Cross Pattern

Example #11

In the following C program, the user can enter number of rows to print the Square 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 = 7, length = 18, max_length = 20;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
{
if(i == 1)
printf("% - 3d",j);
else if(j == n)
printf("% - 3d",C);
else if(i == n)
printf("% - 3d",length--);
else if(j == 1)
printf("% - 3d",max_length--);
else
printf("   ");
}
printf("\n");
}
return 0;
}

Output:

Square pattern

Example #12

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:

Vertical Triangle of Numbers

Example #13

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:

vertical triangular

Example #14

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:

Triangle pattern

Example #15

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:

pyramid pattern

Recommended Articles

This is a guide to Number Patterns in C. Here we discuss the introduction and different examples along with the sample code. You can also go through our other suggested articles to learn more –

  1. Star Patterns In c++
  2. Patterns in JavaScript
  3. Patterns in C#
  4. Number Patterns in Java

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More