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:

Program Output:

Similar Number Patterns:

Similar Star pattern programs :

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

Leave a Reply