C Program to Print Number Pattern
Last Updated :
15 Dec, 2024
A number pattern involves printing numbers in a specific arrangement or shape, often in the form of a pyramid, triangle, or other geometric shapes. They are great for practicing loops and conditional statements. In this article, we will learn how to print different number patterns in C.
Rhombus Number Pattern
The Rhombus Number Pattern pattern looks like a rhombus shape which is nothing but a slightly slanted square filled with numbers starting from 1 to the given size of the edge.
C
#include <stdio.h>
int main() {
int n = 5;
// Outer loop to iterate through each row
for (int i = 0; i < n; i++) {
// First inner loop to print white spaces
for (int j = 0; j < n - i - 1; j++)
printf(" ");
// Second inner loop to print number in each row
for (int k = 0; k < n; k++)
printf("%d ", k + 1);
printf("\n");
}
return 0;
}
Output 1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
Right Half Pyramid Pattern
The Right Half Pyramid Pattern pattern forms a right-angled triangle with numbers increasing along each row and aligned to the left.
C
#include <stdio.h>
int main() {
int n = 5;
// First loop for printing rows
for (int i = 0; i < n; i++) {
// Second loop for printing number in each rows
for (int j = 0; j <= i; j++)
printf("%d ", j + 1);
printf("\n");
}
return 0;
}
Output1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Left Half Pyramid Pattern
The Left Half Pyramid Pattern pattern is similar to the right half pyramid but the numbers are aligned to the left.
C
#include <stdio.h>
int main() {
int n = 5;
// Outer loop is for printing the rows
for (int i = 0; i < n; i++) {
// First inner loop for printing leading whitespaces
for (int j = 0; j < 2 * (n - i) - 2; j++)
printf(" ");
// Second inner loop for printing numbers
for (int k = 0; k <= i; k++)
printf("%d ", k + 1);
printf("\n");
}
return 0;
}
Output 1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Inverted Right Half Pyramid
An Inverted Right Half Pyramid Pattern is a variation of right half triangle pattern flipped 180 vertically.
C
#include <stdio.h>
int main() {
int n = 5;
// Outer loop to print all rows
for (int i = 0; i < n; i++) {
// Inner loop to print the numbers in each row
for (int j = 0; j < n - i; j++)
printf("%d ", j + 1);
printf("\n");
}
}
Output1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Inverted Left Half Pyramid Pattern
An Inverted left Half Pyramid Pattern is a variation of left half triangle pattern flipped 180 vertically.
C
#include <stdio.h>
int main() {
int n = 5;
// Outer loop for printing all rows
for (int i = 0; i < n; i++) {
// First inner loop for printing white spaces
for (int j = 0; j < 2 * i; j++)
printf(" ");
// Second inner loop for printing numbers
for (int k = 0; k < n - i; k++)
printf("%d ", k + 1);
printf("\n");
}
return 0;
}
Output1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Full Pyramid Pattern
A Full Pyramid Pattern consists of numbers arranged in a symmetric equilateral triangular shape. The largest row is at the bottom, and each row above it contains fewer numbers.
C
#include <stdio.h>
int main() {
int n = 5;
// Outer loop to print all rows
for (int i = 0; i < n; i++) {
// First inner loop to print white spaces
for (int j = 0; j < 2 * (n - i) - 1; j++)
printf(" ");
// Second inner loop to print numbers
for (int k = 0; k < 2 * i + 1; k++)
printf("%d ", k + 1);
printf("\n");
}
return 0;
}
Output 1
1 2 3
1 2 3 4 5
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9
Inverted Full Pyramid Pattern
An Inverted Full Pyramid Pattern is a variation of full pyramid pattern rotated upside down.
C
#include <stdio.h>
int main() {
int n = 5;
// Outer loop for printing all rows
for (int i = 0; i < n; i++) {
// First inner loop for printing leading white
// spaces
for (int j = 0; j < 2 * i; j++)
printf(" ");
// Second inner loop for printing numbers
for (int k = 0; k < 2 * (n - i) - 1; k++)
printf("%d ", k + 1);
printf("\n");
}
}
Output1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7
1 2 3 4 5
1 2 3
1
Pascal’s Triangle Pattern
Pascal’s triangle is a triangular array of numbers where each number is the sum of the two numbers directly above it. The numbers generally are center aligned.
C
#include <stdio.h>
int main() {
int n = 5;
// Outer loop for rows
for (int i = 1; i <= n; i++) {
// First inner loop for leading white spaces
for (int j = 0; j < n - i; j++)
printf(" ");
// Coefficient
int C = 1;
// Second inner loop for printing numbers
for (int k = 1; k <= i; k++) {
printf("%d ", C);
C = C * (i - k) / k;
}
printf("\n");
}
return 0;
}
Output 1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Floyd’s Triangle Pattern
Floyd’s Triangle is a right-angled triangular pattern of numbers, where the numbers are printed sequentially across rows. Each row contains increasing numbers, starting from 1
in the first row, 2,3
in the second row, and so on, until all numbers from 1
to n
(the total count) are printed.
C
#include <stdio.h>
int main() {
int n = 4;
int c = 1;
// Outer loop to print all rows
for (int i = 0; i < n; i++) {
// Inner loop to print abphabet in each row
for (int j = 0; j <= i; j++)
printf("%d ", c++);
printf("\n");
}
return 0;
}
Output1
2 3
4 5 6
7 8 9 10
Similar Reads
Pattern Programs in C
Printing patterns using C programs has always been an interesting problem domain. We can print different patterns like star patterns, pyramid patterns, Floyd's triangle, Pascal's triangle, etc. in C language. These problems require the knowledge of loops and if-else statements. We will discuss the f
15+ min read
C Program For Printing Right Half Pyramid Pattern
A half-right pyramid consists of rows with sequential stars, numbers or characters arranged in a triangular shape. The first row has one character, the second row has two, and so on. The characters are aligned to the left making it similar to the right-angle triangle. In this article, we will learn
5 min read
C Program to Print Pyramid Pattern
In C, a pyramid pattern consists of numbers, stars, or alphabets arranged in a triangular shape. In this article, we will learn how to print different shapes of pyramid patterns using C program. Following are the 6 common pyramid patterns: Right Half Pyramid PatternRight half pyramid pattern looks l
13 min read
C Program to Print Number Pattern
A number pattern involves printing numbers in a specific arrangement or shape, often in the form of a pyramid, triangle, or other geometric shapes. They are great for practicing loops and conditional statements. In this article, we will learn how to print different number patterns in C. Rhombus Numb
6 min read
C Program to Print Continuous Character Pattern
Here, we will see how to print continuous character patterns using a C program. Below are the examples: Input: rows = 5Output: A B C D E F G H I J K L M N O Input: rows = 3Output: A B C D E F There are 2 ways to print continuous character patterns in C: Using for loop.Using while loop. Let's discuss
5 min read
C Program To Print Character Pyramid Pattern
Pyramid patterns is a classic logical programming exercise where a triangular looking pattern is printed by treating the output screen as a matrix and printing a given character. In this article, we will explore how to print various alphabet pyramid patterns using C program. Half Pyramid PatternHalf
4 min read
C Program to Print Right Half Pyramid Pattern
The Right Half Pyramid Pattern is a triangular pattern consists of rows where each row contains an increasing number of characters. The number of characters starts from 1 and increases by 1 in each subsequent row. Characters are aligned to the left, resembling a right-angle triangle with its hypoten
2 min read
C Program To Print Hollow Pyramid Patterns
The Hollow Pyramid patterns are the variation of pyramid patterns where only the outer edges are filled with characters but the interior is left empty. In this article, we will learn how to print different hollow pyramid patterns. There can be 5 hollow pyramid patterns corresponding to each of the n
12 min read
C Program to Print Cross or X Pattern
The Cross or X Pattern is a pattern where characters or stars are printed diagonally from top-left to bottom-right and from top-right to bottom-left, forming an "X" shape. In this article, we will learn how to print this pattern using a C program. Program to Print Cross or X Pattern[GFGTABS] Star Cr
3 min read
Programs to print Interesting Patterns
Program to print the following pattern: Examples : Input : 5 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * This program is divided into four parts. C/C++ Code // C++ program to print // the given pattern #include<io
15+ min read