Results 1 to 3 of 3

Thread: Check whether an integer is a prime number in C

  1. #1
    Join Date
    May 2008
    Posts
    21

    Check whether an integer is a prime number in C

    A prime number is an integer, which is divided only by 1 and itself.

    Algorithm 1: dividers between 2 and N-1 will be tested

    Code:
    /************************** \ 
    * * xyz_abc1.c 
    / * Algorithm: testing all dividers * / 
     # include <stdio.h> 
     int main (void) 
     ( 
        int i, nb, count, test; 
        test = count = 0; 
        printf ( "Enter integer"); 
        if (scanf ( "% d", & nb)! = 1)
          return -1;
        
    for (i = 2; i <nb i + + count + +)
       if (nb% i == 0)
       test = 1;
    if (! test)
      printf ( "% d prime number, many iterations =% d \ n", nb, count);
    else 
      printf ( "% d is not the first number, many iterations =% d \ n", nb, count);
    return 0;
    )

  2. #2
    Join Date
    May 2008
    Posts
    21

    Dividing peers will not be tested, research is limited to odd dividers

    Algorithm 2: Dividing peers will not be tested, research is limited to odd dividers

    Code:
    /************************** \ 
    
       * * xyz_abc1.c 
      \ **************************/ 
    
      / * Algorithm: exclude even numbers and 
    
       * Test all dividers * / 
      # include <stdio.h> 
    
    
      int main (void) 
      ( 
        int i, nb, count, test; 
        test = count = 0; 
        printf ( "Enter integer"); 
        if (scanf ( "% d", & nb)! = 1) 
          return -1; 
    
        if (nb% 2 == 0) 
                test = 1; 
        else ( 
            for (i = 3; i <nb i + = 2, count + +) 
              if (nb% i == 0) 
                test = 1; 
        ) 
        if (! test) 
                printf ( "% d prime number, many iterations =% d \ n", 
                                nb, count); 
       else 
                printf ( "% d is not the first number, many iterations =% d \ n", nb, count); 
        return 0; 
      )

  3. #3
    Join Date
    May 2008
    Posts
    21

    Dividing up the odd square root of N will be tested

    Algorithm 3: Dividing up the odd square root of N will be tested

    Dividing up the odd square root of N will be tested

    Code:
    /************************** \ 
    
       * * xyz_abc3.c
      \ **************************/ 
    
      / * Algorithm: exclude even numbers and 
    
       * Test all dividers to the square root * / 
      # include <stdio.h> 
      # include <math.h> 
    
      int main (void) 
      ( 
        int i, nb, count, test limits; 
        test = count = 0; 
        printf ( "Enter integer"); 
        if (scanf ( "% d", & nb)! = 1) 
          return -1; 
        limit = sqrt (nb) + 1; 
    
        if (nb% 2 == 0) 
                test = 1; 
        else ( 
            for (i = 3; i <limit i + = 2, count + +) 
              if (nb% i == 0) 
                test = 1; 
        ) 
        if (! test) 
                printf ( "% d prime number, many iterations =% d \ n", nb, count); 
       else 
                printf ( "% d is not the first number, many iterations =% d \ n", nb, count); 
        return 0; 
      )

Similar Threads

  1. Replies: 2
    Last Post: 24-01-2012, 01:06 PM
  2. Shell program for the prime number
    By Rup_me in forum Software Development
    Replies: 5
    Last Post: 14-12-2009, 10:24 AM
  3. program to check prime number in java
    By Bansi_WADIA in forum Software Development
    Replies: 3
    Last Post: 24-11-2009, 11:02 PM
  4. Prime Number programme in C#
    By Harshit in forum Software Development
    Replies: 2
    Last Post: 14-11-2008, 01:09 PM
  5. Prime number
    By mad4jack in forum Software Development
    Replies: 3
    Last Post: 03-10-2008, 09:24 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,493,879.34165 seconds with 17 queries