Example:
Input N: 5
Output:
Required knowledge
Basic C programming, LoopLogic to print given pattern
Before I discuss the logic to print the given pattern I recommend you to have a careful eye on to the pattern. Following observations can be made about the pattern.- It contains of N rows (where N is the total rows to be printed).
- Each row contains exactly double column as in previous row. And the first row contains 1 column.
- As per each column contains an integer from 1 to 9 in increasing order. When the number count reaches 10 it restarts to 1.
Program to print the given pattern
/** * C program to print the given number pattern */ #include <stdio.h> int main() { int i, j, N, colCount, value; colCount = 1; value = 1; printf("Enter rows: "); scanf("%d", &N); for(i=1; i<=N; i++) { for(j=1; j<=colCount; j++) { if(value == 10) value = 1; //Restart at 10 printf("%d ", value); value++; } printf("\n"); //Increases the total number of columns by 2 colCount *= 2; } return 0; }
Output
Enter rows: 5
1
2 3
4 5 6 7
8 9 1 2 3 4 5 6
7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4
1
2 3
4 5 6 7
8 9 1 2 3 4 5 6
7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4
Screenshot
Happy coding ;)