Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 15th, 2012  |  syntax: C  |  size: 0.89 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main (void)
  5.  
  6. {
  7.         unsigned int p, d, n, N, count = 0;
  8.         unsigned char isPrime;
  9.  
  10.         printf ("Give me the interval: ");
  11.         scanf ("%i %i", &p, &n);
  12.  
  13.                 if ( p == 0 || p == 1 )
  14.                         p = 2;
  15.  
  16.                 if ( p > n )
  17.                         printf ("The first number must be less or equal to the second number!\n");
  18.                
  19.                 else {
  20.  
  21.                         printf ("The prime numbers between %u and %u are:\n\n", p, n);
  22.  
  23.                 for ( ; p <= n; ++p ) {
  24.                 isPrime = 1;
  25.                                 N = sqrt(p);
  26.  
  27.                 for ( d = 2; d <= N; ++d ) {
  28.                         if ( p % d == 0 ) {
  29.                                 isPrime = 0;
  30.                                                                 break;
  31.                                                 }
  32.                                 }
  33.  
  34.                 if ( isPrime != 0 ) {
  35.                         printf ("%i ", p);
  36.                                         ++count;
  37.                                    }
  38.                      }
  39.         printf ("\n\nPrime numbers in total: %i\n", count);
  40.                 }
  41.  
  42.         return 0;
  43. }