Given a number and we need to find whether a given number is prime or not.
So what is prime Number?
Prime number is a number that is divisble by itself and 1 .
for example-2,3,5,7,11 …. are prime number because they are divisble by 1 and itself. .
Examples:
input:5
output:true
input:7
output:true
input:10
output:false
#include<iostream> using namespace std; int main() { int num; cout<<"Enter a number to check Number is prime or not : "<<endl; cin>>num; bool flag=0; for(int i=2;i<=num/2;i++) { if(num%i==0)//checking whether number have any other divisor { flag =1;//if divisor present then number is not a prime number break; } } if(flag==0) { cout<<num<<" is a prime number"<<endl; } else { cout<<num<<" is not a prime number"<<endl; } }
Output
Enter a number to check Number is prime or not : 5 5 is a prime number
Expected Time Complexity-O(N) where N is the given number.
Expected space Complexity-O(1).