# C++ program to print all Even and Odd numbers from 1 to N

Given a number N, the task is to print N even numbers and N odd numbers from 1.

Examples:

```Input: N = 5
Output:
Even: 2 4 6 8 10
Odd: 1 3 5 7 9

Input: N = 3
Output:
Even: 2 4 6
Odd: 1 3 5
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. For Even numbers:
• Even number are numbers that are divisible by 2.
• To print even numbers from 1 to N, traverse each number from 1.
• Check if these numbers are divisible by 2.
• If true, print that number.
2. For Odd numbers:
• Odd number are numbers that are not divisible by 2.
• To print Odd numbers from 1 to N, traverse each number from 1.
• Check if these numbers are not divisible by 2.
• If true, print that number.
3. Below is the implementation of the above approach:

 `// C++ program to print all Even ` `// and Odd numbers from 1 to N ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to print even numbers ` `void` `printEvenNumbers(``int` `N) ` `{ ` ` `  `    ``cout << ``"Even: "``; ` `    ``for` `(``int` `i = 1; i <= 2 * N; i++) { ` ` `  `        ``// Numbers that are divisible by 2 ` `        ``if` `(i % 2 == 0) ` `            ``cout << i << ``" "``; ` `    ``} ` `} ` ` `  `// Function to print odd numbers ` `void` `printOddNumbers(``int` `N) ` `{ ` ` `  `    ``cout << ``"\nOdd: "``; ` `    ``for` `(``int` `i = 1; i <= 2 * N; i++) { ` ` `  `        ``// Numbers that are not divisible by 2 ` `        ``if` `(i % 2 != 0) ` `            ``cout << i << ``" "``; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` ` `  `    ``int` `N = 5; ` ` `  `    ``printEvenNumbers(N); ` `    ``printOddNumbers(N); ` ` `  `    ``return` `0; ` `} `

Output:

```Even: 2 4 6 8 10
Odd: 1 3 5 7 9
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.