Thread: Calculating next prime number

  1. #1
    Registered User anilemon's Avatar
    Join Date
    Apr 2006
    Location
    Australia
    Posts
    3

    Calculating next prime number

    Hi

    In one of my tutorials, I needed to write a program that prompts the user to enter a number, then calculate and show the prime number following the input.

    The program needs to prompt the user again and again until the user enters 0.

    Since its a beginner unit, no error checking is needed (so if you enter "a", the program does go crazy )

    My program kind of works, but I'm not sure how to make it so that the program prompts the user for an input again if they input a negative number, since right now, I have it exit with a negative number.

    Also, my lecturer once that "exit()" is evil... so is there a better way to make the program exit when the user inputs a 0?

    I'm sure this isn't the best solution to the problem, so if you have any suggestions on how to improve it, it would be very much appreciated

    Code:
    /* File name: prime.c
     * This program takes a number, then displays the next prime number.
     */
    
    #include <stdio.h>
    #include <math.h>
    
    #define TRUE 1
    #define FALSE 0
    
    /* Prototypes */
    int isPrime(int input); 
    int nextPrimeNumber();
    
    int main(void){
    
        nextPrimeNumber();
    
    }
    
    /* Function for determining the next prime number */
    int nextPrimeNumber(){
        
        int inputNumber = 1;
        int nextPrimeNumber;
    
        while(inputNumber != 0){
            printf("\nPlease enter an integer: ");
            scanf("%d", &inputNumber);
    
            if(inputNumber<=0){
                printf("The number you have entered is zero or negative.\n");
                exit(0);
            }
    
            nextPrimeNumber = inputNumber + 1;
    
            /* if the number is even, make it odd (2 is special case) */
            if(nextPrimeNumber%2 == 0 && nextPrimeNumber != 2){ 
                nextPrimeNumber+=1;
            }
    
            /* while its not a prime number, check the next odd number */
            while(!isPrime(nextPrimeNumber)){
                nextPrimeNumber+=2;
            }
    
            printf("The prime number following %d is %d\n", inputNumber, nextPrimeNumber);
        }
        return 0;
    }
    
    
    /* Function that checks whether or not a given number is
     * a prime number or not. 
     */
    int isPrime(int input){
        int i;
    
        int prime = TRUE;
    
        if(input == 2){
            return TRUE;
        }
    
        if(input%2 == 0 || input <= 1){
            prime = FALSE;
        } else {
            for(i=3; i<=sqrt(input); i+=2){
                if(input%i == 0){
                    prime = FALSE;
                }
            }
        }
        return prime;
    }
    Thanks very much

    Ani

  2. #2
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Code:
    if(input == 0)
    {
        break; // to interrupt the while-loop
    }
    
    if(input < 0)
    {
        cout << "Wrong input!";
        cin.ignore(1000, '\n');
        cout << "Press enter to try again!";
        cin.get();
        continue; // start while-loop from beginning.
    }
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by Ideswa
    Code:
    if(input == 0)
    {
        break; // to interrupt the while-loop
    }
    
    if(input < 0)
    {
        cout << "Wrong input!";
        cin.ignore(1000, '\n');
        cout << "Press enter to try again!";
        cin.get();
        continue; // start while-loop from beginning.
    }
    C++ on C board

    ssharish2005

  4. #4
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    oops!!
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    95
    1) In the function isPrime. once you find that the number is not
    prime, there is no reson to keep the for loop.
    2) I don't know why you where told that the exit function is
    bad.
    3) If you want the user to keep input even with negative
    numbers, why not let the while loop do it ?
    Code:
        while(inputNumber != 0)

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    #include <limits.h>
    #include <string.h>
    #include <time.h>
    #include <assert.h>
    
    
    #define TRUE 1
    #define FALSE 0
    
    /* Prototypes */
    int isPrime(int input); 
    int nextPrimeNumber(void);
    
    
    
    
    int main(void)
    {
    
    nextPrimeNumber();
    
    printf("Press enter to end ....\n");
    getchar();
    return(0);
    }
    
    
    /* Function for determining the next prime number */
    int nextPrimeNumber(void)
    {
    int inputNumber = 1;
    int nextPrimeNumber;
    
    while(inputNumber != 0)
        {
        printf("\nPlease enter an integer [0 - exit]: ");
        scanf("%d", &inputNumber);
    
        if ( inputNumber < 0 )
          printf("The number you have entered is negative. Try again\n");
        else if ( inputNumber > 0 )
           {
           nextPrimeNumber = inputNumber + 1;
    
           /* if the number is even, make it odd (2 is special case) */
           if(nextPrimeNumber%2 == 0 && nextPrimeNumber != 2)
              nextPrimeNumber+=1;
    
           /* while its not a prime number, check the next odd number */
           while(!isPrime(nextPrimeNumber))
               nextPrimeNumber+=2;
    
           printf("The prime number following %d is %d\n", inputNumber, nextPrimeNumber);
           }
        }
    return 0;
    }
    
    
    /* Function that checks whether or not a given number is
     * a prime number or not. 
     */
    int isPrime(int input)
    {
    int i;
    int prime = TRUE;
    
    
    if(input == 2)
      return TRUE;
    
    if(input%2 == 0 || input <= 1)
       prime = FALSE;
    else 
       {
       for(i=3; i<=sqrt(input) && prime == TRUE  ; i+=2)
          if(input%i == 0)
             prime = FALSE;
       }
    return prime;
    }


    why is the function nextPrimeNumber return a integer ?
    You dont the check the returned value in main anyway.

  6. #6
    Registered User anilemon's Avatar
    Join Date
    Apr 2006
    Location
    Australia
    Posts
    3
    :P I'm new to C, so I'm not sure what the normal return types should be.

    But after I went through your code a bit, I get it now

    Thanks very much for your time
    Ani

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    95
    A function may return any kind of data type.
    But it may return no value at all. In this case the function
    is prototyped as returing "void", but you may use the
    return without a value.


    The next code is an exapmle.

    Code:
    /* the prototype : */
    void sum(int a, int b);
    
    int main(void)
    {
    int result;
    
    
    /* The compiler will warn you, on the next line :
        result = sum(3,5);
    */
    /* sum returns with no value */
    sum(3, 5);
    }
    
    void sum(int a, int b)
    {
    if ( a < 0 || b < 0 )
       return; /* return with no value */
    printf("a+b = %d\n", a+b);
    }

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    and main should return a value as well
    Code:
    int main(void)
    {
    int result;
    
    
    /* The compiler will warn you, on the next line :
        result = sum(3,5);
    */
    /* sum returns with no value */
    sum(3, 5);
     return 0;
    }
    ssharish2005
    Last edited by ssharish2005; 04-17-2006 at 10:28 AM.

  9. #9
    Registered User anilemon's Avatar
    Join Date
    Apr 2006
    Location
    Australia
    Posts
    3
    Oh ok, so if a function does not return anything with "return", then it should start with "void".

    Thanks
    Ani

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. More fun with the chinese prime number algorigthm
    By kryonik in forum C Programming
    Replies: 4
    Last Post: 09-01-2005, 04:41 PM
  3. Replies: 3
    Last Post: 03-29-2005, 04:24 PM
  4. prime number finder.
    By Aalmaron in forum C++ Programming
    Replies: 2
    Last Post: 03-05-2004, 04:56 PM
  5. Prime Number?
    By cprogramnewbie in forum C Programming
    Replies: 8
    Last Post: 03-16-2002, 12:18 AM