Determine Whether a Given Number Is Prime - C Data Type

C examples for Data Type:int

Introduction

A prime number is a positive whole number that is exactly divisible only by 1 and itself.

Demo Code

#include <stdio.h>
#include <math.h>

int main()/*from  w  w w .jav  a2s  .  co m*/
{
   int flag, isPrime;
   long int number = 367, result, i;

   if (number == 2) {
      printf("\n2 is a prime number\n\n");
      return 0;
   }

   isPrime = 1;
   result = ceil(sqrt(number));

   for (i = 2; i <= result; i++) {
      if ((number % i) == 0) {
         isPrime = 0;
         break;
      }
   }

   if (isPrime)
      printf("\n%ld is a prime number\n\n", number);
   else
      printf("\n%ld is not a prime number\n\n", number);


   return(0);
}

Result


Related Tutorials