Prime Number Evaluation Using Function
In this program we are going to evaluate if a number is prime number or not using functions in C programming.
To learn basics of function in C programming Click Here.
Example :
#include<stdio.h>
#include<conio.h>
//Declaration of prime funtion
int prime(int x);
void main()
{
int x;
clrscr();
//Taking number from the user to check if it's prime or not
printf("\nEnter a number:");
scanf("%d",&x);
//calling prime number for true of false
if(prime(x))
printf("\nNumber is Prime");
else
printf("\nNumber is not prime");
getch();
}
//Definition of prime function
int prime(int n)
{
int checkprime=0,i;
for(i=2;i<n;i++)
{
if(n%i==0)
checkprime=1;
}
if(checkprime==0)
return 1;
else
return 0;
} |
Output :
For More GTU C Programming Lab Experiments Click Here