Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to print numbers columns wise in C
In C programming, we can create patterns where natural numbers are arranged column-wise in a triangular format. This pattern displays numbers in columns, where each row contains one more number than the previous row.
Pattern Description
The pattern arranges numbers as follows −
1 2 6 3 7 10 4 8 11 13 5 9 12 14 15
Each number is placed based on its column position, creating a triangular arrangement where numbers flow vertically in columns.
Algorithm
The algorithm uses the following approach −
- i represents the current row (1 to 5)
- j represents the current column position in that row
- k stores the number to be printed
- For each row i, initialize k = i
- For each column j in that row, print k and update k = k + (5 - j)
Example
Here's the complete C program to print numbers in column-wise pattern −
#include <stdio.h>
int main() {
int i, j, k;
printf("Printing the Numbers in Columns wise:<br><br>");
for(i = 1; i <= 5; i++) {
k = i;
for(j = 1; j <= i; j++) {
printf("%d ", k);
k += 5 - j;
}
printf("<br>");
}
return 0;
}
Printing the Numbers in Columns wise: 1 2 6 3 7 10 4 8 11 13 5 9 12 14 15
How It Works
The pattern generation works as follows −
- Row 1: k=1, prints 1
- Row 2: k=2, prints 2, then k=2+(5-1)=6, prints 6
- Row 3: k=3, prints 3, then k=3+(5-1)=7, prints 7, then k=7+(5-2)=10, prints 10
- This pattern continues for all rows
Conclusion
This program demonstrates how to create column-wise number patterns using nested loops and mathematical calculations. The key is understanding how the increment value changes based on the current column position.
