Friday, March 4, 2011

User-Defined Functions

Write a function prime that returns 1 if its argument is a prime number and returns zero otherwise.

#include <stdio.h>
prime(int x);
void main()
{
int x,y;
clrscr();
printf("Enter a number:");
scanf("%d",&x);
y=prime(x);
printf("%d",y);
getch();
}
prime(int x)
{
int i,y;
for(i=2;i<=x-1;i++)
{
if(x==2)
y=1;
if(x%i==0)
{
y=0;
break;
}
else
y=1;
}
return(y);
}

OUTPUT
Enter a number : 5
1

Enter a number : 6
0

No comments:

Post a Comment