C++ Program to check number is prime or not using call by Reference


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


Call by Reference

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


Example: How to check number is prime or not using call by Reference in C++

Run it Now
#include<iostream>
using namespace std;

void check(int n, int *p) {
    int i;
    for (i = 2; i < n; i++) {
        if (n % i == 0) {
            *p = 0;
        }
    }
}

int main() {

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

    check(n, &p);

    if (p == 1) {
        cout << "\nThe Number is Prime :" << n;
    } else {
        cout << "\nThe Number is Not Prime :" << n;
    }

    return 0;
}

Output:

Enter a Number :7
The Number is Prime :7