Pattern 40 : C Program to print the below Odd Even number pattern
Odd Even Number Pattern Program :
Write a C Program to print below Odd Even Number Pattern on the console using the loops.
Here are the couple of examples of expected pattern outputs.
Example 1:
Enter how many rows you want : 5
1
2 4
1 3 5
2 4 6 8
1 3 5 7 9
Example 2:
1
2 4
1 3 5
2 4 6 8
1 3 5 7 9
2 4 6 8 10 12
1 3 5 7 9 11 13
2 4 6 8 10 12 14 16
1 3 5 7 9 11 13 15 17
2 4 6 8 10 12 14 16 18 20
Odd Even Number Pattern Program Logic:
From the above examples, We can see the first row of the pattern have Odd values(i.e 1) and second row of the pattern have even values (i.e 2 4), Similarly 3rd row have Odd values ( i.e 1 3 5 ), so on.
So we need to print Odd values at Odd rows and Even values at Even Rows.
Each Odd row starts with 1 and goes to next odd value by adding 2 to it.
Similarly each Even Row starts with 2 and goes to next even value by adding 2 to it.
Here is the program logic
- We have two loops one is Outer for loop and second one is Inner for loop
- The outer for loop represents the rows, So it will start from 1 and goes till the ‘n’ ( The ‘n’ is number of rows)
- For each Iteration of outer loop, The inner loop will goes from 1 to ‘i’ ( Here ‘i’ is the outer loop iterator)
- Before printing the values, We check if the present row is even or Odd by using ( i % 2 == 0 ) Logic. If the output is 0 means our present row is Even, Otherwise Odd row.
- Once we find the row type ( Here variable ‘k’ is rowtype) , If it is Odd row we will start with 1 and keep on adding value 2 to it. So we always get the odd number
- Similarly, If the Row type ( Here variable ‘k’) is Even, Then our printing starts from 2 and prints desired even numbers until we reach (j<=i)
📢 Also Check All Pattern Programs:
Odd Even 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 |
#include<stdio.h> int main() { int i,j,k,n; // Take input from the User printf("Enter how many rows you want : "); scanf("%d", &n); // Iterate from 1 to n ( i.e Number of Rows) for(i=1; i<=n; i++) { // We need to print Even Numbers on Even Rows // Odd numbers on Odd Rows // So check if the row is even or odd // If even start with 2 // If odd start with 1 if( i%2 == 0) k=2; else k=1; // We have triangle shape so start form 1 go till 'i' // Now at each print increase the value by 2 for(j=1; j<=i; j++) { printf(" %d", k); k+=2; // k=k+2 } 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
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