How to display prime numbers in C

A prime number is any number that admits two integer and positive divisors, which are the 1 and the number itself. 1 is not considered a prime number because it admits that a divisor. The 0 too since it is divisible by all numbers.


C/C++ - Prime numbers

Two ways to check if p is a prime number:
  1. The rest of the division is zero for all lower numbers except the 1 and the number p .
  2. Wilson's theorem states that an integer is prime if and only if the factorial of p-1 is equivalent to -1 modulo p.
    (p - 1)! + 1    0 (mod p).
    Source:  http://fr.wikipedia.org/wiki/Th%C3%A9or%C3%A8me_de_Wilson
We'll use the first method.

Program that checks if an integer is a prime number:

using namespace std;
#include< iostream>
#include< stdlib.h>

int main()
{
    int nb,r=0;
    cost< < "Enter a number: "; cin> > nb;
     
    for(int i = 1 ; i <= nb ; i++ )
    {
        if(nb % i == 0)
        {
            r++;
        }          
    }
     
    if(r> 2)
        cost< < nb< < " is not a prime number"< < endl;
    else
        cost< < nb< < " is a prime number"< < endl;
    system("pause");
}

Program that checks whether the numbers entered are prime:

#include< stdio.h>
#include< stdlib.h>


int main()
{
    int number=1 counter=0;
    int i,r,n=100;

    while(counter< n){//the first n
          r=0;
//to count the number of divisors
          number++;
          for (i=1 ; i<=number; i++)
          {
              if ((number%i)==0
                r++;
         }
          if(r==2)//The prime number is divided into 1 and itself
          {
            printf(" %d \n", number);
            //we increment the counter
            counter++;
         }
   }
    system("pause");
}

Program that displays all  Prime Numbers  Lower Levels  at n:

#include< stdio.h>
#include< stdlib.h>


int  main()
{
      int  number=1 counter=0;
      int  i,r,n=100;

    printf("Prime numbers less than %d are:\n",n);
      while(
number  < n){//as long as number < n then
          r=0;
//to count the number of divisors
          number++;
          for  (i=1 ; i<=number; i++)
          {
              if  ((number%i)==0
                r++;
         }
          if(r==2)//The prime number is divided into 1 and itself
          {
            printf(" %d \n", number);
         }
   }
    system("pause");
}

Procedure:

Prime numbers less than 100 are:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73,79, 83, 89, 97.