Techy Blitz

The science of today is the technology of tomorrow.

Breaking

Tuesday, 14 September 2021

C program to check prime number

 



Prime Number formula: For a number p>3; if  p² - 1 is divisible by 24 then it is prime.

 

#include<iostream>
using namespace std;

int main()
{
int n;
cin>>n;
if(n>=1){
if(n==2 || n==3){
cout<<n<<" is prime";
}
else if(((n*n)-1)%24==0){
cout<<n<<" is prime";
}
else{
cout<<n<<" is not prime";
}
}
else{
cout<<"Enter a valid number";
}
return 0;
}