Prime number program problem

This is a discussion on Prime number program problem within the C Programming forums, part of the General Programming Boards category; Hello, My program has to determine if a number that I enter is a prime number - which I believe ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    Prime number program problem

    Hello,

    My program has to determine if a number that I enter is a prime number - which I believe I have done. But if the number is not a prime number, it should tell me the highest divisor of this particular number. At the moment, my program gives all the divisors if the number is not a prime. And the input range must be between 1 and MAXINT. Can anyone tell me how to get MAXINT? how to declare it?

    Thanks

    Code:
    #include <stdio.h>
    #include <process.h>
    
    void main()
    {
       char  your_name[12];
       int   number;
       int   i;
       int   remainder;
       int   not_divisible = 1;
    
       printf("Hello!, please enter your name: ");
       scanf("%s", your_name);
       printf("Enter a number: ");
       scanf("%d", &number);
    
       printf("\nThe number you entered is: %d", number);
       printf("\n");
    
       for( i = 2; i < 10; i++ )
       {
          remainder = number % i;
          if(remainder == 0 )
          {
             not_divisible = 0;
             printf("\This number is divisible by %d", i);
             printf(" and it is not a prime number");
          }
       }
       if(not_divisible == 1) printf("This a prime number!");
       printf("\n");
       printf("Good-bye %s", your_name);
       printf("\n");
       system("pause");
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    If you're referring to the one in stddef.h then you don't need to define it. It's called INT_MAX and you can use it anywhere in your program.

    If you'd like to define your own then somewhere in your program (near the top) type #define MAXINT 5 or whatever you want MAXINT to be.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by itsme86
    If you're referring to the one in stddef.h then you don't need to define it. It's called INT_MAX and you can use it anywhere in your program.
    I believe you mean limits.h

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    Thanks for the tip, however, I just saw an example where value.h is declared in order to use MAXINT
    And can anyone help me out on how I can just display the highest divisor instead of all of them?

    Thanks,

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Real quick:
    Code:
    #include <stdio.h>
    #include <process.h>
    
    int main(void)
    {
       char  your_name[12];
       int   number;
       int   i;
       int   remainder;
       int   not_divisible = 1;
       int   highest_div=1;
    
       printf("Hello!, please enter your name: ");
       scanf("%s", your_name);
       printf("Enter a number: ");
       scanf("%d", &number);
    
       printf("\nThe number you entered is: %d", number);
       printf("\n");
    
       for( i = 2; i < 10; i++ )
       {
          remainder = number % i;
          if(remainder == 0 )
          {
             not_divisible = 0;
             highest_div = i;
          }
       }
       if(not_divisible == 1) printf("This a prime number!");
       else
             printf("This number is divisible by %d and thus is not prime\n", highest_div);
    
       printf("\n");
       printf("Good-bye %s", your_name);
       printf("\n");
       system("pause");
    }

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Oops! limits.h, sorry. That's what I get for not double-checking.

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by Guti14
    Thanks for the tip, however, I just saw an example where value.h is declared in order to use MAXINT
    And can anyone help me out on how I can just display the highest divisor instead of all of them?

    Thanks,
    Code:
    #include <stdio.h>
    
    int main(void) // main() returns an int - always 
    {
        char your_name[12];
        int number;
        int i;
        int remainder;
        int not_divisible = 1;
    
        printf("Hello!, please enter your name: ");
        scanf("%s", your_name);
        printf("Enter a number: ");
        scanf("%d", &number);
    
        printf("\nThe number you entered is: %d", number);
        printf("\n");
    
        for (i = 2; i < 10; i++) {
    	remainder = number % i;
    	if (remainder == 0) {
    	    not_divisible = 0;
    	}
        }
    
    /* Get your printf() statements out of the for loop */
        
      printf("\nThis number is divisible by %d", i);
        printf(" and it is not a prime number");
    
        if (not_divisible == 1)
    	printf("This a prime number!");
        printf("\n");
        printf("Good-bye %s", your_name);
        printf("\n");
        
        return 0;
    
    }
    And value.h is not part of the C standard library

    edit::

    ack - the above only is to show how to display one result instead of all of them.
    Last edited by kermit; 08-05-2004 at 10:01 PM.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    It's funny how I was taught with void main() and why is it a bad idea to have printf in a loop?
    I think I need a good tutorial, can you direct me to a website?

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    How about the FAQ on this very same website? http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    EDIT: Here's a key quote:
    What's the deal with void main()

    Under regular function calling/returning in C and C++, if your don't ever want to return anything from a function, you define it's return type as void. For example, a function that takes no arguments, and returns nothing can be prototyped as:

    void foo(void);

    A common misconception is that the same logic can be applied to main(). Well, it can't, main() is special, you should always follow the standard and define the return type as int. There are some exceptions where void main() is allowed, but these are on specialised systems only. If you're not sure if you're using one of these specialised systems or not, then the answer is simply no, you're not. If you were, you'd know it.

    Be warned that if you post your "void main" code on the forums, you're going to get told to correct it. Responding with "my teacher said it's OK" is no defence; teachers have a bad habit of being wrong. Be safe, and post only standard code, and you'll find people concentrate on answering your other problems, rather than waste time telling you about this type of thing.
    Last edited by itsme86; 08-05-2004 at 10:14 PM.

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by kermit
    Code:
        for (i = 2; i < 10; i++) {
    	remainder = number % i;
    	if (remainder == 0) {
    	    not_divisible = 0;
    	}
        }
    This isn't right.

    Actually you want to go to number - 1, not some fixed maximum. For example, try this with 5, and your program will see that 5 is divisible by 5, so it will erroneously set "not_divisible" to 0, and when other logic is fixed, it will report that 5 is not a prime.

    Also to find the biggest divisor, you want something like this:

    Code:
    int biggest = 1;
    at the beginning, then make the loop look like


    Code:
     for (i = 2; i < number - 1; i++) {
    	remainder = number % i;
    	if (remainder == 0) {
    	    biggest = i;
    	}
        }
    So that the variable "biggest" holds the largest divisor found as it steps through the loop.

    Then, outside the loop,

    see whether (biggest == 1). If it is, this is a prime number (by definition), otherwise the largest divisor is equal to the value of "biggest". So then print the appropriate message and say goodnight.

    A final note: you really need to protect the program from people with long names. What if someone enters a name with 12 or more characters? Your array is only good for 11 characters (+ the terminating '\0').


    Dave
    Last edited by Dave Evans; 08-05-2004 at 10:40 PM.

  11. #11
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    If you're looking for the highest divisor, then why not just count backwards
    in your loop. Something like this:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int isPrime ( int n )
    {
      int i;
    
      for (i = n/2; i > 1; i--) {
        if ((n % i) == 0) {
          break;
        }
      }
    
      printf("Largest divisor = %d\n", i);
    
      return (i == 1);
    }
    
    int main ( void )
    {
      int n = 11;
    
      if (isPrime(n)) {
        printf("%d is prime\n", n);
      }
    
      return 0;
    }
    DavT
    -----------------------------------------------

  12. #12
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by Guti14
    It's funny how I was taught with void main() and why is it a bad idea to have printf in a loop?
    I think I need a good tutorial, can you direct me to a website?
    It is not a bad idea to use a printf() statement in a for loop, as long as that is what you need. You had written this:

    And can anyone help me out on how I can just display the highest divisor instead of all of them?
    In your case, your printf() statements were inside the for loop, so they executed every time through the loop, as opposed to just once, if they were outside of the loop. Of course, as Dave pointed out, there were other problems with your code, so that you could not find a prime number.

    Another site you might want to look at for a tutorial can be found here.

    ~/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 02:42 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 11:13 AM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. Replies: 3
    Last Post: 01-14-2003, 10:34 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM

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