Pattern 41: Print Numbers pattern program in C
Print Numbers Pattern on Console:
Write a C Program to print numbers pattern given below using the C Language loops.
We have created the same pattern earlier but we used another method in that program. Please check that method at following link – Pattern 39: 1-0 Number Pattern in C Programming
Example 1:
Enter how many rows you want : 5
1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
Example 2:
Enter how many rows you want : 10
1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
1 0 1 0 1 0
1 0 1 0 1 0 1
1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0
Print Numbers Pattern Program Logic:
- We need two For loops, The Outer for loop and Inner for loop. The Outer for loop helps us to track the rows and Inner for loop is useful to track the column values.
- The outer for loop will iterate from the ‘1’ to the number of rows i.e ‘n’
- The Inner for loop goes from 1 to ‘i’ ( Here ‘i’ is the outer loop iterator ) So stopping inner loop once we reached ‘i’ creates the Triangle shape.
- Now we got the desired shape, We need to print the values now
- If we see above example Patterns, Each row always starts with 1 and then second value is opposite to it i.e 0. And third value is opposite to 0. which is 1 and so on
- So We need to start the each row value with 1 and then Apply Logical Not Operator (!) on the values for each iteration to get the desired value.
- Which will give us 1 0 1 0 .. etc pattern. And we need stop the inner loop once we reached the ( j<=i ) ( Here ‘j’ is inner loop iterator and ‘i’ is Outer loop iterator)
Also Check All Pattern Programs:
Program to print numbers pattern:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include<stdio.h> int main() { int i,j,k,n; // Take input number of Rows from the User printf("Enter how many rows you want : "); scanf("%d", &n); // Iterate from 1 to 'n' to create the 'n' of rows for(i=1; i<=n; i++) { // Take a number 'k' to track the values of '1's and '0's k=0; // Inner Loop will go from 1 to 'i' ( 'i' is outer loop iterator) for(j=1; j<=i; j++) { // We need start with 1, So apply Logical Not('!') Operator // Which converts k from '0' --> '1' // So print that converted value. // Repeat for all iterations k = !k; printf(" %d", k); } printf("\n"); } return 0; } |
Program Output:
Similar Number Patterns:
- Basic Number Pattern program.
- Triangle Number pattern using for loops.
- Number Pattern 3.
- C Program to print below pattern
- 5 4 3 2 1
5 4 3 2
5 4 3
5 4
5 - C Program to print below pattern.
- 1
2 3
4 5 6
7 8 9 10 - C Program to print Below Pattern,
- 5
4 5
3 4 5
2 3 4 5
1 2 3 4 5 - C Program to print below pattern.
- 1
2 1
3 2 1
4 3 2 1
5 4 3 2 1 - C Program to print below pattern.
- 5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
- C Program to print below Number pattern.
- 1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
- C Program to print below Number pattern.
- 5
4 4
3 3 3
2 2 2 2
1 1 1 1 1
- CLICK here for the Below Pattern
- 5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
- C Program to print below pattern
- 1 1 1 1 1
2 2 2 2
3 3 3
4 4
5
- C Program to print below pattern
- 1
0 1
0 1 0
1 0 1 0
1 0 1 0 1
- C Program to print below pattern.
- 1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
- C Program to print below pattern.
- 1
2 4
1 3 5
2 4 6 8
1 3 5 7 9
Similar Star pattern programs :
- Basic star pattern program (The square pattern with stars).
- Triangle star pattern program.
- Triangle star pattern program with Spaces.
- Pyramid star pattern.
- Reverse Pyramid program.
- Diamond Star pattern program.
- star pattern program.
- K-Star pattern program.
- Empty Rhombus program.
- X-Star pattern program
- More Star Patterns