Recent Posts

9-latest-250px-course

Write a C program to Check whether a number can be expressed as a sum of two prime numbers

Write a C program to Check whether a number can be expressed as a sum of two prime numbers



Program:-     

#include <stdio.h>
int sum_of_two_primes(int n);

int main()
{
int n, i;
printf("Enter the number: ");
scanf("%d", &n);
int flag = 0;
for(i = 2; i <= n/2; ++i)
{
// Condition for i to be prime
if (sum_of_two_primes(i) == 1)
{
if (sum_of_two_primes(n-i) == 1)
{
printf("\n%d can be expressed as the sum of %d and %d\n\n", n, i, n - i);
flag = 1;
}
}
}

if (flag == 0)
printf("%d cannot be expressed as the sum of two prime numbers\n", n);

return 0;
}

//function to check if a number is prime or not
int sum_of_two_primes(int n)
{
int i, isPrime = 1;
for(i = 2; i <= n/2; ++i)
{
if(n % i == 0)
{
isPrime = 0;
break;
}
}
return isPrime;
}

Output:-

....
SHARE

Milan Tomic

Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment