Skip to content

Instantly share code, notes, and snippets.

@complxalgorithm
Created May 8, 2016 07:10
Show Gist options
  • Save complxalgorithm/19e1d7563d180b826aa750139eba3970 to your computer and use it in GitHub Desktop.
Save complxalgorithm/19e1d7563d180b826aa750139eba3970 to your computer and use it in GitHub Desktop.
C program that checks whether or not an input number is prime.
/* To check if a number is prime or not */
#include <stdio.h>
#include <ctype.h>
int main()
{
int n, i, flag=0;
printf(“Enter a positive integer: ”);
scanf(“%d”, &n);
if (n<1 || isalpha(n))
{
printf(“Invalid input.”);
break;
}
for (i=1; i<=n/2; i++)
{
if (n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf(“%d is a prime number.”, n);
else
printf(“%d is not a prime number.”, n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment