Write a c program to calculate all the possible pairs of two prime numbers that
ID: 671854 • Letter: W
Question
Write a c program to calculate all the possible pairs of two prime numbers that add up to the given integer. This program reads a positive integer from user and checks whether that number can be expressed as the sum of two prime numbers. To perform this task, two user-defined functions named prime and is sum of two primes are required. Function prime checks if a given integer is prime; and is sum of two primes checks whether a number can be expressed as the sum of two prime numbers. Following is a sample output:
Enter a positive integer:
34 = 3 + 31
34 = 5 + 29
34 = 11 + 23
34 = 17 + 17
Explanation / Answer
#include<stdio.h>
int isPrime(int number)
{
for(int i=2;i<number/2;i++)
{
if(number%i == 0)
return 0;
}
return 1;
}
void sumOfTwoPrimes(int number)
{
for(int i=2;i<number/2+1;i++)
{
if(isPrime(i)&&isPrime(number-i))
printf("%d = %d + %d ",number,i,number-i);
}
}
int main(void)
{
printf("Input an integer : ");
int number;
scanf("%d",&number);
sumOfTwoPrimes(number);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.