Skip to content

Instantly share code, notes, and snippets.

@nalin-adh
Created November 10, 2015 16:52
Show Gist options
  • Save nalin-adh/d7eb282b3ae144ebfddf to your computer and use it in GitHub Desktop.
Save nalin-adh/d7eb282b3ae144ebfddf to your computer and use it in GitHub Desktop.
A C++ program that uses function prime() for testing prime. Function should take integer argument and return Boolean value.
#include<iostream>
#include<conio.h>
using namespace std;
bool prime(int n);
int main()
{
int n;
cout<<"Enter any number : ";cin>>n;
if(prime(n)==1)
cout<<n<<" is a prime number.";
else
cout<<n<<" is not a prime number.";
getch();
return(0);
}
bool prime(int n)
{
if(n==0||n==1||n==2)
return(0);
else
{
for(int i=2;i<=(n/2);i++)
{
if(n%i==0)
return(0);
}
return(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment