C Program to Print Number Pattern
The idea of pattern based programs is to understand the concept of nesting of for loops and how and where to place the alphabets / numbers / stars to make the desired pattern.
Write a program to print the pattern of numbers in the following manner using for loop
1
232
34543
4567654
567898765
In almost all types of pattern programs, two things that you must take care:
- No. of lines
- If the pattern is increasing or decreasing per line?
Implementation
C
// C program to illustrate the above // given pattern of numbers. #include <stdio.h> int main() { int n = 5, i, j, num = 1, gap; gap = n - 1; for (j = 1; j <= n; j++) { num = j; for (i = 1; i <= gap; i++) printf ( " " ); gap--; for (i = 1; i <= j; i++) { printf ( "%d" , num); num++; } num--; num--; for (i = 1; i < j; i++) { printf ( "%d" , num); num--; } printf ( "\n" ); } return 0; } |
Output
1 232 34543 4567654 567898765
Time Complexity: O(n2), where n represents the given input.
Auxiliary Space: O(1), no extra space is required, so it is a constant.