Answer to Question #284360 in C for Venkatesh

Question #284360

C program to check whether a number can be expressed as sum of two prime number using function

1
Expert's answer
2022-01-03T04:28:02-0500
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;
}




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS