C++ Program to check number is even or odd using call by Reference


In this program, You will learn how to check number is even or odd using call by Reference in C++.


Call by Reference

void check(int n, int *p) {
   //statement
}

Example: How to check number is even or odd using call by Reference in C++.

#include<iostream>
using namespace std;

void check(int n, int *p) {

    if( n % 2 == 0){
        *p = 1;
    }else{
        *p = 0;
    }
}

int main() {

    int n, p = 1;
    cout<< "Enter a number:";
    cin>> n;

    check(n, &p);

    if (p == 1) {
        cout<< "Number is even:"<< n;
    } else {
        cout<< "Number is odd:"<< n;
    }

    return 0;
}

Output:

Enter a number:13
Number is odd:13

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.