# [C] for loop problem

• 10-16-2011
SirSig
[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; }```
• 10-16-2011
GReaper
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.
• 10-16-2011
SirSig
Quote:

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!
• 10-16-2011