prime numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • peppercolt
    New Member
    • Oct 2006
    • 1

    prime numbers

    I need help with a program that will take an integer and output whether or not the number is prime or composite.
  • D_C
    Contributor
    • Jun 2006
    • 293

    #2
    There are much better ways to do this, but here's a quick lazy method.
    Code:
    bool isPrime(int input)
    {
      int divisor;
      for(divisor = 2; divisor < (input/2); divisor++)
        if((input % divisor) == 0)
          return true;
      return false;
    }

    Comment

    • sowmyth
      New Member
      • Oct 2006
      • 19

      #3
      this is how i did in my school
      #include<stdio. h>
      main( ){
      int n,i,flag=0;
      printf("enter a number");
      scanf("%d",&n);
      for(i=2;i<n-1;i++){
      if(n%i==0){
      flag=1;
      break;
      }
      }
      if (flag==0)
      printf("the number is prime");
      }

      Comment

      • D_C
        Contributor
        • Jun 2006
        • 293

        #4
        Actually you only need to check 2, then the odd numbers from 3 to floor(sqrt(n)).
        The reason is because if x, such that 1 < x < sqrt(number), is a factor, then (number/x) is a factor, and sqrt(number) <= number/x < number. Factors come in pairs, one will be less than or equal to sqrt(number), the other will be greater than or equal to sqrt(number).

        Comment

        • deepakwasake
          New Member
          • Oct 2006
          • 4

          #5
          tel me how to write program on pallindrome?
          tel me program which have output like
          1
          12
          123
          1234
          12345

          Comment

          Working...