Introduction
Number pattern programs are a common way to learn how to control loops and nested loops in C programming. These patterns involve printing numbers in different shapes like triangles, pyramids, and diamonds. They are often used in coding interviews and help strengthen your understanding of loops, conditional statements, and logical thinking. This post covers several number pattern programs in C with detailed explanations and code.
1. Right-Angled Triangle Number Pattern
This pattern forms a right-angled triangle using numbers, where the number of columns in each row increases by one, starting from 1.
Example:
1
12
123
1234
12345
C Program:
#include <stdio.h>
int main() {
int rows = 5;
// Outer loop for rows
for (int i = 1; i <= rows; i++) {
// Inner loop to print numbers
for (int j = 1; j <= i; j++) {
printf("%d", j);
}
printf("\n"); // Move to the next line after each row
}
return 0;
}
Explanation:
- The outer loop controls the number of rows.
- The inner loop prints numbers from 1 to
i
for each row, forming a right-angled triangle with increasing numbers.
2. Inverted Right-Angled Triangle Number Pattern
This pattern prints an inverted right-angled triangle where the number of columns (numbers) decreases with each row.
Example:
12345
1234
123
12
1
C Program:
#include <stdio.h>
int main() {
int rows = 5;
// Outer loop for rows
for (int i = rows; i >= 1; i--) {
// Inner loop to print numbers
for (int j = 1; j <= i; j++) {
printf("%d", j);
}
printf("\n"); // Move to the next line after each row
}
return 0;
}
Explanation:
- The outer loop starts from
rows
and decreases with each iteration. - The inner loop prints numbers starting from 1 to
i
, wherei
is the current row number.
3. Pyramid Number Pattern
A pyramid pattern where numbers increase in each row, and the numbers are center-aligned.
Example:
1
12
123
1234
12345
C Program:
#include <stdio.h>
int main() {
int rows = 5;
// Outer loop for rows
for (int i = 1; i <= rows; i++) {
// Print leading spaces for alignment
for (int j = i; j < rows; j++) {
printf(" ");
}
// Print numbers in increasing order
for (int j = 1; j <= i; j++) {
printf("%d", j);
}
printf("\n"); // Move to the next line after each row
}
return 0;
}
Explanation:
- The first inner loop prints spaces to center-align the numbers.
- The second inner loop prints numbers from 1 to
i
for each row, forming a pyramid.
4. Inverted Pyramid Number Pattern
An inverted pyramid where numbers start wide and decrease in each row.
Example:
12345
1234
123
12
1
C Program:
#include <stdio.h>
int main() {
int rows = 5;
// Outer loop for rows
for (int i = rows; i >= 1; i--) {
// Print leading spaces for alignment
for (int j = i; j < rows; j++) {
printf(" ");
}
// Print numbers in decreasing order
for (int j = 1; j <= i; j++) {
printf("%d", j);
}
printf("\n"); // Move to the next line after each row
}
return 0;
}
Explanation:
- The first inner loop prints spaces for alignment.
- The second inner loop prints numbers, starting from 1 and decreasing with each row, forming an inverted pyramid.
5. Floyd’s Triangle
Floyd's Triangle is a right-angled triangle where the numbers are printed consecutively starting from 1.
Example:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
C Program:
#include <stdio.h>
int main() {
int rows = 5;
int num = 1; // Starting number
// Outer loop for rows
for (int i = 1; i <= rows; i++) {
// Inner loop to print consecutive numbers
for (int j = 1; j <= i; j++) {
printf("%d ", num);
num++; // Increment the number
}
printf("\n"); // Move to the next line after each row
}
return 0;
}
Explanation:
- The outer loop controls the number of rows.
- The inner loop prints consecutive numbers for each row, starting from 1 and increasing until the triangle is formed.
6. Diamond Number Pattern
A diamond-shaped number pattern where numbers increase in the upper half and decrease in the lower half symmetrically.
Example:
1
121
12321
1234321
123454321
1234321
12321
121
1
C Program:
#include <stdio.h>
int main() {
int rows = 5;
// Print the upper half of the diamond
for (int i = 1; i <= rows; i++) {
// Print leading spaces
for (int j = i; j < rows; j++) {
printf(" ");
}
// Print increasing numbers
for (int j = 1; j <= i; j++) {
printf("%d", j);
}
// Print decreasing numbers
for (int j = i - 1; j >= 1; j--) {
printf("%d", j);
}
printf("\n");
}
// Print the lower half of the diamond
for (int i = rows - 1; i >= 1; i--) {
// Print leading spaces
for (int j = rows; j > i; j--) {
printf(" ");
}
// Print increasing numbers
for (int j = 1; j <= i; j++) {
printf("%d", j);
}
// Print decreasing numbers
for (int j = i - 1; j >= 1; j--) {
printf("%d", j);
}
printf("\n");
}
return 0;
}
Explanation:
- The first part prints the upper half of the diamond using nested loops.
- The second part prints the lower half of the diamond, which mirrors the upper half.
7. Pascal’s Triangle
Pascal’s Triangle is a triangle where each number is the sum of the two numbers directly above it.
Example:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
C Program:
#include <stdio.h>
int main() {
int rows = 5;
for (int i = 0; i < rows; i++) {
// Print leading spaces
for (int j = 0; j < rows - i - 1; j++) {
printf(" ");
}
int num = 1; // First number in the row
for (int j = 0; j <= i; j++) {
printf("%d ", num);
num = num * (i - j) / (j + 1); // Calculate next number in Pascal's Triangle
}
printf("\n"); // Move to the next line
}
return 0;
}
Explanation:
- The outer loop controls the rows.
- The inner loop prints the numbers in Pascal's Triangle, calculating each number as the sum of the two above it.
8. Number Palindrome Pyramid
This pyramid pattern forms a palindromic sequence of numbers in each row.
Example:
1
121
12321
1234321
123454321
C Program:
#include <stdio.h>
int main() {
int rows = 5;
// Outer loop for rows
for (int i = 1; i <= rows; i++) {
// Print leading spaces
for (int j = i; j < rows; j++) {
printf(" ");
}
// Print increasing numbers
for (int j = 1; j <= i; j++) {
printf("%d", j);
}
// Print decreasing numbers
for (int j = i - 1; j >= 1; j--) {
printf("%d", j);
}
printf("\n"); // Move to the next line
}
return 0;
}
Explanation:
- The first inner loop prints spaces for centering the pyramid.
- The second and third inner loops print increasing and decreasing numbers to form the palindrome in each row.
9. Repeated Number Triangle Pattern
This pattern forms a triangle where each row contains repeated numbers.
Example:
1
22
333
4444
55555
C Program:
#include <stdio.h>
int main() {
int rows = 5;
// Outer loop for rows
for (int i = 1; i <= rows; i++) {
// Inner loop to print repeated numbers
for (int j = 1; j <= i; j++) {
printf("%d", i); // Print the row number
}
printf("\n"); // Move to the next line after each row
}
return 0;
}
Explanation:
- The outer loop controls the number of rows.
- The inner loop prints the same number multiple times, equal to the current row number.
10. Hollow Square Number Pattern
This pattern forms a hollow square where the boundary is filled with numbers, and the inside is hollow.
Example:
11111
1 1
1 1
1 1
11111
C Program:
#include <stdio.h>
int main() {
int size = 5;
// Outer loop for rows
for (int i = 1; i <= size; i++) {
// Inner loop for columns
for (int j = 1; j <= size; j++) {
if (i == 1 || i == size || j == 1 || j == size) {
printf("1");
} else {
printf(" "); // Print spaces inside the square
}
}
printf("\n"); // Move to the next line after each row
}
return 0;
}
Explanation:
- The outer loop controls the rows, and the inner loop controls the columns.
- Numbers are printed only at the boundaries of the square, and spaces are printed inside.
Conclusion
These 10 C number pattern programs help in understanding nested loops, conditional statements, and controlling print patterns in C. These number patterns are often asked in coding interviews and help develop problem-solving skills in C programming. By practicing these patterns, you can improve your understanding of loops and how to manipulate output effectively.
Comments
Post a Comment
Leave Comment