Saturday, 12 March 2011

Write a function prime that returns 1 if its argument is a prime no. and returns 0 otherwise

#include<stdio.h>
#include<conio.h>
int prime(int);
int main()
{
int n,p;
clrscr();
printf("Enter a number : ");
scanf("%d",&n);
p=prime(n);
if(p==1)
printf("%d is prime\n",n);
else
printf("%d is not prime\n",n);
getch();
return 0;
}
int prime(int n)
{
int i;
for(i=2;i<n;i++)
{
if(n%i==0)
return 0;
}
return 1;
}
Output:
Enter a number : 18
18 is not prime
Enter a number : 5
5 is prime

2 comments: