Inertial Observer Inertial Observer - 1 month ago 5
C Question

How to fix program printing a 0 after the print statement executes in C?

This program tests Goldbach's Conjecture, printing a given even integer as the sum of two primes. After printing the first one, the given integer is to iterate by 2 , then find the sum of two primes for that integer. And so on until the program is interrupted by the user.

This problem is that the program prints a '0' after every executed print statement.

The code:

#include <stdio.h>
#include <math.h>

int GC(int goldsum); //function prototype

int main()
{
int goldsum;

printf("Enter an even integer greater than 5: ");
scanf("%d", &goldsum);

printf("%d\n", GC(goldsum));

goldsum = goldsum + 2;

printf("%d\n", GC(goldsum));

}

int GC(int goldsum) //function definition
{
int i, j; //prime addends
int div1, div2; //divisors
char prime1, prime2;

for (i=2 ;i<goldsum ;i++) //when number is less than goldsum, run this loop iterating by 1
{
prime1 = 1;
for (div1=2 ;div1<i ;div1++) //this loop determines if "i" is prime.
if (i % div1 == 0) //if yes, the prime number is stored in "i"
prime1 = 0;
if (prime1)
{
for (j=3; j<goldsum; j+=2) //when number is less than goldsum, run this loop iterating by 2
{
prime1 = 1;
for (div2=2; div2<j; div2++) //this loop determines if "j" is prime.
if(j % div2 == 0) //if yes, the prime number is stored in "j"
prime1 = 0;
if (prime1)
if (i + j == goldsum) //If i + j = goldsum, it prints the result.

{
printf("%d + %d = %d\n",i ,j , goldsum);
return 0;
}

}
}
}
return 0;
}


The output:

Enter an even integer greater than 5: 10
3 + 7 = 10
0
5 + 7 = 12
0


What I want it to look like:

Enter an even integer greater than 5: 10
3 + 7 = 10
5 + 7 = 12

Answer

use just GC(goldsum); instead of printf("%d\n", GC(goldsum));

Comments