Write a function isprime() which takes a number and returns 1 if the number is prime and 0 otherwise.

  What is a prime number in definition?

A prime number is a whole number is greater than 1whose only factors are 1 and itself. A factors a whole number that can be  divided evenly into another number. The first few prime number are : 2, 3, 5, 7, 11, 13, 17, 19 and  23  etc. 


The following program check the input number is prime or not.

#include<stdio.h>
int isprime(int);
int main()
{
    int num;
    printf("Enter the Number check it
is prime or not :");
    scanf("%d",&num);
    if(isprime(num))
    {
        printf("%d is prime number.\n",
num);
    }
    else
    {
        printf("%d is not a prime number.
        \n",num);
    }
    return 0;
}
int isprime(int n)
{
    int i = 2;
    while(i<n)
    {
        if(n%i==0) /*number is completely
divisible by smaller no.
        so it not a prime number*/
            return 0;
        i++;
    }
    if(i==n) // prime number is divisible
by itself.
        return 1;
}
Output :
Enter the Number check it is prime or
not :7 7 is prime number. Enter the Number check it is prime or
not :9 9 is not a prime number.


The above table show prime number between 1 to 100.


Comments

Popular posts from this blog

18 Amazing programs in C.