Tuesday, June 7, 2016

FIND PRIME NUMBER USING RECURSION

#include <iostream>
#include <string>

using namespace std;


int prime (int i, int n)
{
    if(i==n/2)return 0;
    if(n%i==0)
        return 1;
    prime (i+1,n);
}

int main()
{
    int n, i;
    int found;
    cin>> n;
    found = prime (2 ,n);
    if(found==0)
        cout<< "Prime"<< endl;
    else
        cout<< "Not prime"<< endl;

    return 0;
}

No comments:

Post a Comment