Answers for "check if number is prime in c"

4

prime check in c

int isPrime(long long number) {

	if (number == 0 || number == 1 || (number % 2 == 0 && number > 2)) {
		return 0;
	}

	else {
		for (long long i=3; i <= (long long)sqrt(number)+1; i++) {
			if (number % i == 0) {
				return 0;
			}
		}
		return 1;
	}
}
Posted by: Guest on February-07-2021
0

program to know if a number is prime

#include<bits/stdc++.h>
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";
}
Posted by: Guest on April-30-2020
1

prime number c

#include <stdio.h>
int main()
{
   int i, num, p = 0;
   printf("Please enter a number: \n");
   scanf("%d", &num);
   for(i=1; i<=num; i++)
   {
      if(num%i==0)
      {
         p++;
      }
   }
   if(p==2)
   {
      printf("Entered number is %d "\
             "and it is a prime number.",num);
   }
   else
   {
      printf("Entered number is %d "\
             "and it is not a prime number.",num);
   }
}
Posted by: Guest on June-04-2020
0

check if number is prime in c

#include<stdio.h>
int main()
{
    int num,i,flag=0;
    printf("enter the number:");
    scanf("%d",&num);
    for(i=2;i<=(num/2);i++)
    {
       if(num%i==0)
          flag=1;
    }
    if(flag==0)
        printf("%d is a prime number",num);
    else
        printf("%d is not a prime number",num);
    return 0;
}
Posted by: coder on September-11-2022

Code answers related to "check if number is prime in c"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language