C++ program to print all Even and Odd numbers from 1 to N
Last Updated :
13 Mar, 2023
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 numbers 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 numbers 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++
// C++ program to print all Even
// and Odd numbers from 1 to N
#include <bits/stdc++.h>
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;
}
OutputEven: 2 4 6 8 10
Odd: 1 3 5 7 9
Time Complexity: O(N)
Auxiliary Space: O(1), As constant extra space is used.
Method: Using bitwise & Operator
C++
// C++ program to print all Even
// and Odd numbers from 1 to N
#include <bits/stdc++.h>
using namespace std;
// Function to print even numbers
void printEvenNumbers(int N)
{
cout << "Even: ";
for (int i = 1; i <= 2 * N; i++) {
//checking even or not
if (!(i & 1))
cout << i << " ";
}
}
// Function to print odd numbers
void printOddNumbers(int N)
{
cout << "\nOdd: ";
for (int i = 1; i <= 2 * N; i++) {
// checking odd or not
if (i & 1 != 0)
cout << i << " ";
}
}
// Driver code
int main()
{
int N = 5;
printEvenNumbers(N);
printOddNumbers(N);
return 0;
}
// This code is contributed by vinay Pinjala.
OutputEven: 2 4 6 8 10
Odd: 1 3 5 7 9
Time Complexity: O(n)
Auxiliary Space: O(1), As constant extra space is needed.
Method: Using bitwise | Operator
C++
// C++ program to print all Even
// and Odd numbers from 1 to N
#include <bits/stdc++.h>
using namespace std;
// Function to print even numbers
void printEvenNumbers(int N)
{
cout << "Even: ";
for (int i = 1; i <= 2 * N; i++) {
//checking even or not using bitwise | operator
if ((i | 1)==i+1)
cout << i << " ";
}
}
// Function to print odd numbers
void printOddNumbers(int N)
{
cout << "\nOdd: ";
for (int i = 1; i <= 2 * N; i++) {
//checking odd or not using bitwise | operator
if ((i | 1) == i)
cout << i << " ";
}
}
// Driver code
int main()
{
int N = 5;
printEvenNumbers(N);
printOddNumbers(N);
return 0;
}
// This code is contributed by tvsk.
OutputEven: 2 4 6 8 10
Odd: 1 3 5 7 9
Time Complexity: O(n)
Auxiliary Space: O(1), As constant extra space is needed.
Method: Using Recursion
C++
// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to Print Even- Odd number using Recursion
void printEvenOdd(int n)
{
if (n <= 0)// if n<=0 return because
{ return ; }// we have to find in [1,N]
//recursive call
printEvenOdd(n - 2);
cout << n << " "; //print numbers
}
// Drive Code
int main()
{
int n = 5 ;
cout << "Even: ";
//Function for printing even numbers
printEvenOdd(2*n);
//Function call for printing odd numbers
cout <<endl<< "Odd: ";
printEvenOdd(2*n-1);
return 0;
}
// This code is contributed by nikhilsainiofficial546
OutputEven: 2 4 6 8 10
Odd: 1 3 5 7 9
Time Complexity: O(n)
Auxiliary Space: O(n), because of recursive call
Similar Reads
C++ Program to find sum of even factors of a number Given a number n, the task is to find the even factor sum of a number. Examples: Input : 30 Output : 48 Even dividers sum 2 + 6 + 10 + 30 = 48 Input : 18 Output : 26 Even dividers sum 2 + 6 + 18 = 26 Let p1, p2, ⦠pk be prime factors of n. Let a1, a2, .. ak be highest powers of p1, p2, .. pk respect
3 min read
Print "Even" or "Odd" without using conditional statement Write a program that accepts a number from the user and prints "Even" if the entered number is even and prints "Odd" if the number is odd. You are not allowed to use any comparison (==, <,>,...etc) or conditional statements (if, else, switch, ternary operator,. Etc). Method 1 Below is a tricky
4 min read
C++ Program to Print Even Numbers in an Array Given an array of numbers, the task is to print all the even elements of the array. Let us understand it with few examples mentioned below. Example: Input: num1 = [2,7,6,5,4] Output: 2, 6, 4 Input: num2 = [1,4,7,8,3] Output: 4, 81. Using for loop Approach: Iterate each element in the given using for
5 min read
C++ Program to Check Whether Number is Even or Odd A number is even if it is completely divisible by 2 and it is odd if it is not completely divisible by 2. In this article, we will learn how to check whether a number is even or odd in C++.ExamplesInput: n = 11Output: OddExplanation: Since 11 is not completely divisible by 2, it is an odd number.Inp
3 min read
C++ Program for Frequencies of even and odd numbers in a matrix Given a matrix of order m*n then the task is to find the frequency of even and odd numbers in the matrix Examples: Input : m = 3, n = 3 { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } Output : Frequency of odd number = 5 Frequency of even number = 4 Input : m = 3, n = 3 { 10, 11, 12 }, { 13, 14, 15 }, { 16, 1
3 min read
C++ Program to Rotate all odd numbers right and all even numbers left in an Array of 1 to N Given a permutation arrays A[] consisting of N numbers in range [1, N], the task is to left rotate all the even numbers and right rotate all the odd numbers of the permutation and print the updated permutation. Note: N is always even.Examples: Input: A = {1, 2, 3, 4, 5, 6, 7, 8} Output: {7, 4, 1, 6
2 min read