program to find prime numbers in c code example

Example 1: prime numbers in c

#include
main() {
    int n, i, count=0;
    printf("Enter Number");
    scanf("%d",&n);
   
    for ( i = 1; i<= n; i++) {
        if (n % i == 1) {
            count++;
        }
    }
    if( count == 2) {
        printf("Prime Number");
    }
    else {
        printf("Not A  Prime Number");
    }
}

Example 2: program to know if a number is prime

#include
using namespace std;
bool Is_Prime(long long x){
	if(x%2==0)return false;
	for(int i=3;i*i<=x;i+=2)
		if(x%i==0)return false;
	return true;
}
int main(){
	long long x;
	cin>>x;
	if(Is_Prime(x))cout<<"Is Prime";
	else cout<<"Is not Prime";
}

Tags:

Misc Example