Introduction to Computer Science - Fall 2004

/*

    Author Yana
    This program checks whether input number is prime or not. Version 1.
    Input: A positive integer n>=2.
    Output: A message whether n is prime or not.
    Algorithm:
    1. Read input number
    2. Check and count all possible divisors from 1 up to n.
    3. n is prime if it is has exactly two different divisors - 1 and itself.
    Assumption: input valid.

*/

#include<stdio.h >
int main(){
    int n;        /* Input number. */
    int divisor;     /* Possible divisor tested. */
    int counter = 0;     /* Number of divisors found. */

    /* Read input */
    printf( "Please enter a number ");
    scanf("%d", &n);

    /* Check and count all possible divisors. */
    for (divisor = 1; divisor <= n; divisor ++){
      if (n % divisor == 0)
        counter ++;
    }

    /* Print output */
    if (counter == 2)
      printf("The number %d is a prime number \n", n);
    else
      printf("The number %d is not a prime number\n", n);

    return 0;
}