[C] for loop problem

This is a discussion on [C] for loop problem within the C Programming forums, part of the General Programming Boards category; Hi, I'm working on a program which is supposed to identify prime numbers and output prime pairs (prime numbers which ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    9

    [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. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    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.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    9
    Quote Originally Posted by GReaper View Post
    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. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    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 ).
    Devoted my life to programming...

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by GReaper View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop problem
    By roelof in forum C Programming
    Replies: 9
    Last Post: 05-25-2011, 06:19 AM
  2. Problem with a loop
    By wiggsfly in forum C Programming
    Replies: 5
    Last Post: 11-30-2008, 11:45 AM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  5. WHILE LOOP problem
    By jrahhali in forum C++ Programming
    Replies: 5
    Last Post: 03-08-2003, 05:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21