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

16 Dec, 2019
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

Approach:
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.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.Below is the implementation of the above approach:// C++ program to print all Even// and Odd numbers from 1 to N  #include <bits/stdc++.h>using namespace std;  // Function to print even numbersvoid 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 numbersvoid 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 codeint main(){      int N = 5;      printEvenNumbers(N);    printOddNumbers(N);      return 0;}Output:Even: 2 4 6 8 10 
Odd: 1 3 5 7 9
My Personal Notes
arrow_drop_upSave
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.
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.
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.Below is the implementation of the above approach:// C++ program to print all Even// and Odd numbers from 1 to N  #include <bits/stdc++.h>using namespace std;  // Function to print even numbersvoid 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 numbersvoid 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 codeint main(){      int N = 5;      printEvenNumbers(N);    printOddNumbers(N);      return 0;}Output:Even: 2 4 6 8 10 
Odd: 1 3 5 7 9
My Personal Notes
arrow_drop_upSave
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.
Below is the implementation of the above approach:
// C++ program to print all Even// and Odd numbers from 1 to N  #include <bits/stdc++.h>using namespace std;  // Function to print even numbersvoid 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 numbersvoid 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 codeint main(){      int N = 5;      printEvenNumbers(N);    printOddNumbers(N);      return 0;}
Even: 2 4 6 8 10 
Odd: 1 3 5 7 9

C++
School Programming
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Friend class and function in C++
Sorting a vector in C++
Convert string to char array in C++
Python Dictionary
Reverse a string in Java
Interfaces in Java
Operator Overloading in C++
Overriding in Java