This program takes a positive integer from user and checks whether that number can be expressed as the sum of two prime numbers. If that number can be expressed as sum of two prime numbers then, that number is expressed as sum of two prime numbers in output.
#include <stdio.h>
#include <conio.h>
int prime(int n);
int main()
{
int n, i, flag=0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2; i<=n/2; ++i)
{
if (prime(i)!=0)
{
if ( prime(n-i)!=0)
{
printf("%d = %d + %d\n", n, i, n-i);
flag=1;
}
}
}
if (flag==0)
printf("%d can't be expressed as sum of two prime numbers.",n);
getch();
return 0;
}
int prime(int n) /* Function to check prime number */
{
int i, flag=1;
for(i=2; i<=n/2; ++i)
if(n%i==0)
flag=0;
return flag;
}
OUTPUT:
No comments:
Post a Comment