C Program to Check Number is Prime or Not using Function

C Program to Check Number is Prime or Not using Function:
If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().

#include<stdio.h>
main()
{
    int n,a;
    int prime(int);
    printf("Enter a number to find prime or not\n");
    scanf("%d",&n);
    printf("If %d is Prime it will print 1\n",n);
    printf("If %d is Not-Prime it will print 0\n",n);
    a=prime(n);
    printf("Answere is : %d\n",a);

}

int prime(int n)
{
    int d=2,b;
    if(n==2)
    {
        b=1;
        return(b);
    }
    else
    {
     if(n%2==0)
     {
        b=0;
        return(b);
     }

     else
     {
        b=1;
        return(b);
     }
    }
}

 

OUTPUT:

Enter a number to find prime or not
7
If 7 is Prime it will print 1
If 7 is Not-Prime it will print 0
Answere is : 1
--------------------------------------------------
Enter a number to find prime or not
8
If 8 is Prime it will print 1
If 8 is Not-Prime it will print 0
Answere is : 0

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.