even and odd numbers c++ code example

Example 1: number is even or odd c++

if ( n % 2 == 0)
        cout << n << " is even.";
    else
        cout << n << " is odd.";

Example 2: return odd numbers c++

#include<iostream>
int copyOdd (int A[], int sizeA, int B[]);
int main ()
{
    int arr [] {1,2,3,4,5,6,7,8,9,10};
    int Odd [5] {0,0,0,0,0};
    copyOdd(arr,9,Odd);
    

    for(int i = 0 ; i < 5; ++i)
    {
        std::cout<<Odd[i]<<' ';
    }
    std::cout<<std::endl;

    return 0 ;

}

int copyOdd (int A[], int sizeA, int B[])
{   
    int j {0};
    B [5];
    for(int i  = 0 ; i < sizeA ; ++i)
    {
        if(A[i]%2 == 1)
            B[j++] = A[i];
            
    }

    return B[5];
}

Tags:

Cpp Example