1. [C] for loop problem

Hi, I'm working on a program which is supposed to identify prime numbers and output prime pairs (prime numbers which are 1 integer value away from each other).

My function for checking prime numbers seems to be working correctly but i can't seem to make the program output the list of prime pairs. I'm guessing I set up the for loop wrong or the if else statements.

Any help is much appreciated!

Code:
```/*-------------
Include Section
--------------*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
#include <time.h>

#define TRUE 1
#define FALSE 0

/*------------------Main Functions------------------
Purpose: Prompts user to input
Returns: Singal to the OS
--------------------------------------------------*/

int
main(void)
{

int code, userinput, i, positiveinteger;

printf("To generate prime pairs < 10000, enter 1\nTo check for a prime, enter 2\nTo terminate the program, enter 0: ");
scanf("%d", &code);

while (code != 0) {
if (code == 1) {
printf("\nEnter a positive integer > 5 and < 10000: ");
scanf("%d", &userinput);
printf("\nThe prime pairs up to %d are: \n", userinput);
for (i = 3; i <= userinput; i++) {
if (isprime(userinput) == TRUE && isprime (userinput + 2) == TRUE) {
printf("\n%*d", 5, isprime(userinput));
printf("  ,%*d\n", 5, isprime(userinput + 2));
}
}
}
else
if (code == 2) {
printf("\nEnter a positive integer > 5 and < 2,000,000: ");
scanf("%d", &positiveinteger);
if (isprime(positiveinteger) == TRUE)
printf("\n%d is a prime.\n\n", positiveinteger);
else
printf("\n%d is not a prime.\n\n", positiveinteger);
}
else
printf("\nWrong code.\n\n");

printf("To generate prime pairs < 10000, enter 1\nTo check for a prime, enter 2\nTo terminate the program, enter 0: ");
scanf("%d", &code);
}
printf("\n*** Program Terminated ***\n");
return (EXIT_SUCCESS);
}

/*------------------Prime Functions------------------
Purpose: Check if input is a prime number
Returns: True or False
--------------------------------------------------*/

int isprime(int userinput)
{
int counter;

for (counter = 2; counter < userinput; counter++) {
if (userinput % counter == 0)
return FALSE;
}
return TRUE;
}```

2. This is the correct way to do it:
Code:
```for (i = 3; i <= userinput - 2; i++) {
if (isprime(i) == TRUE && isprime (i + 2) == TRUE) {
printf("\n%*d", 5, i);
printf("  ,%*d\n", 5, i + 2);
}
}
}```
Remember that this is an extremely inefficient way, you could just generate an array of primes from 2 to "userinput" and iterate the array looking for adjacent pairs, much faster.

3. Originally Posted by GReaper
This is the correct way to do it:

Remember that this is an extremely inefficient way, you could just generate an array of primes from 2 to "userinput" and iterate the array looking for adjacent pairs, much faster.
Ohhh, I see why you use i rather than userinput.

I agree that it is inefficient, thats why the assignment cannot check higher values, but in class we have not learned arrays.

Thank you for all your help!

4. Only 2 and 3 are primes that are +1 away from each other. All other pairs can be no closer than +2, which is what you meant, I believe.

5. I'm not sure what he meant... He may be asking for pairs that are have +/-2 difference ( 3 5, 5 7, etc ) or for pairs with index difference +/-2 ( primes[0] primes[2], primes[1] primes[3], etc ).

6. Originally Posted by GReaper
I'm not sure what he meant... He may be asking for pairs that are have +/-2 difference ( 3 5, 5 7, etc ) or for pairs with index difference +/-2 ( primes[0] primes[2], primes[1] primes[3], etc ).
Actually he was pretty specific about 1 integer value... and that only happens with 2 and 3... other than 2 all primes are odd numbers meaning the closest they get is 2 interger values ... eg. 3 and 5. This because all even numbers are divisible by 2.