Advertisement
arif334

Prime

May 25th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. /**
  2.     A program to determine whether a number is prime or not.
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <math.h>
  7.  
  8. int main()
  9. {
  10.     int n, i, j;
  11.     bool is_prime;
  12.  
  13.     printf("Enter a number: ");
  14.     scanf("%d", &n);
  15.  
  16.     is_prime = true;
  17.     for(i=2, j=sqrt(n); i<=j; i++)
  18.     {
  19.         if(n % i == 0)
  20.         {
  21.             is_prime = false;
  22.             break;
  23.         }
  24.     }
  25.  
  26.     if(is_prime == true) printf("Prime.");
  27.     else printf("Not prime.");
  28.  
  29.     return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement