Pattern 38: 0 1 Number Pattern Program in C Language
0 1 Number Pattern Program:
Write a C Program to print below 0 1 number pattern on the console using for loops.
Program will accepts the input number from the user and prints the pattern
Example 1:
Enter how many rows you want : 5
1
0 1
0 1 0
1 0 1 0
1 0 1 0 1
Example 2:
Enter how many rows you want : 10
1
0 1
0 1 0
1 0 1 0
1 0 1 0 1
0 1 0 1 0 1
0 1 0 1 0 1 0
1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0 1
The 0 1 Number Pattern Logic:
If we see the above example programs. The Pattern starts with number 1 and then goes to number 0. Then it goes back to number 1. so on.
So here we are printing a number ( i.e 1 ) and then we will apply the Logical NOT Operator ( ! ) it. In the next iteration, We will again do the same thing applying NOT operator ( ! ) so the value will be converted from 0 to 1.
So logic will be
- As we need Triangle shape, The pattern need to start with printing 1 value at row 1 and 2 values at row 2 and so on.
- We need two loops, Outer loop and Inner Loop. So the Outer Loop will be start from 1 to n. Representing the number of rows.
- For each Iteration of Outer loop, The inner loop will need to go from 1 to 'i' ( 'i' outer loop iterator ). Which gives us the triangle shape
- Okay, Now we know how to get the triangle shape. Now we need to print the values.
- We will start with the number 1 at the first row. Then after printing the value, We will convert the 1 to 0 using the not ( '!' ) operator. Which converts the 1 to 0 and 0 to 1
- Then at each print apply the not operator (!) to the above number. So we will always have the correct number to print at next iteration.
- Repeat the above steps until we reach the number of rows ( 'n' )
📢 Collection of Pattern Programs:
0 1 Number Pattern Program:
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 30 31 32 33 34 |
#include<stdio.h> int main() { int i,j,n; // Take the number of rows as the input printf("Enter how many rows you want : "); scanf("%d",&n); // Lets start with 1 // After printing the value then Negate the value '!' int k=1; // The Outer loop goes from 1 to n // So It is the number of Rows for(i=1;i<=n;i++) { // Our Pattern is in Triangle Shape // So we need to stop the loop when i and j becomes equal // Start j from 1 and go until we reach 'i' for(j=1;j<=i;j++) { // Print the value printf(" %d",k); // Now Negate the value. k=!k; } printf("\n"); } return 0; } |
Program Output:
Similar Number Patterns:
- Come Together – The Collection of all Pattern Programs in C – SillyCodes
- 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
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
12345
5432
123
21
1