Sunday, March 17, 2013

Function To Check Prime

#include<iostream>
using namespace std;
int checkprime(int);
int main()
{
    int n;
    cout<<"Enter a number:";
    cin>>n;
    if(n==0||n==1)
    cout<<"Neither Prime nor Composite!!!!!";
    else{
    if(checkprime(n)==1)
    cout<<"\nPrime!!!!";
    else
    cout<<"\nNot Prime!!!!!!!";
    }
    return 0;
}
int checkprime(int a)
{
    int i,flag=1;
    for(i=2;i<=a/2;++i)
    {
        if(a%i==0)
        {
            flag=0;
            break;
        }
    }
    return flag;
}

0 comments:

Post a Comment