Skip to content

Instantly share code, notes, and snippets.

@adityasuseno
Created July 15, 2019 14:30
Show Gist options
  • Save adityasuseno/3c85e824333e4953bab9b2db3426521b to your computer and use it in GitHub Desktop.
Save adityasuseno/3c85e824333e4953bab9b2db3426521b to your computer and use it in GitHub Desktop.
Checking and Counting Prime Numbers in C
#include<stdio.h>
int isPrime(int n)
{
if (n < 2)
return 0;
else if (n == 2)
return 1;
else
{
int i;
int p;
for(i = 2; i < n; i++)
{
p = n % i;
if (p == 0)
{
return 0;
break;
}
}
return 1;
}
}
main()
{
int c;
int x;
int ans;
for(c = 2; c < 2500001; c++)
{
ans = isPrime(c);
if(ans == 1)
x++;
}
printf("%d \n", x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment