Skip to content

Instantly share code, notes, and snippets.

@wasit-shafi
Created May 28, 2020 16:48
Show Gist options
  • Save wasit-shafi/85c325bd416dc58d58790c1e9a7d43f3 to your computer and use it in GitHub Desktop.
Save wasit-shafi/85c325bd416dc58d58790c1e9a7d43f3 to your computer and use it in GitHub Desktop.
Prime Number Program
#include<stdio.h>
#include<math.h>
int main()
{
int n, i, half, isPrime = 1, square_root;
printf("Enter Value of n... ");
scanf("%d", &n);
square_root = sqrt(n);
if(n > 1)
{
for(i = 2 ; i <= square_root ; i++) // or for(i = 2 ; i * i <= n ; i++)
{
if(n % i == 0)
{
isPrime = 0;
break;
}
}
}
else
isPrime = 0;
if(isPrime)
printf("%d is a Prime Number.", n);
else
printf("%d is not a Prime Number.", n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment