C program to check whether a number can be expressed as sum of two prime number using function
int IsPrime(int n)
{
int i, FlagPrime = 1;
if (n == 0 || n == 1) FlagPrime=0;
else
{
for(i = 2; i <= n/2; ++i)
{
if(n % i == 0) {FlagPrime = 0; break;}
}
}
return (FlagPrime);
}
int main()
{
int n=0, i, FlagPrime = 0;
while(n<=0)
{
printf("\n\tEnter a number (>0): "); scanf("%d", &n);
}
for (i = 2; i <= n / 2; ++i)
{
if (IsPrime(i) == 1)
{
if (IsPrime(n - i) == 1)
{
printf("\n\t%d = %d + %d\n", n, i, n - i);
FlagPrime = 1;
}
}
}
if (FlagPrime == 0) printf("\n\t%d cannot be expressed as the sum of two prime numbers.", n);
return 0;
}
Comments
Leave a comment