Thursday, 27 August 2015

Write a C Program To Write function prime that returns 1 if its argument is a prime no. and returns 0 otherwise (PRIME NUMBER)

Write a C Program To Write function prime that returns 1 if its argument is a prime no. and returns 0 otherwise (PRIME NUMBER)
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  int prime(int);  
4:  int main()  
5:  {  
6:        int n,p;  
7:        clrscr();  
8:        printf("Enter a number : ");  
9:        scanf("%d",&n);  
10:        p=prime(n);  
11:        if(p==1)  
12:              printf("%d is prime\n",n);  
13:        else  
14:              printf("%d is not prime\n",n);  
15:        getch();  
16:        return 0;  
17:  }  
18:  int prime(int n)  
19:  {  
20:        int i;  
21:        for(i=2;i<n;i++)  
22:        {  
23:              if(n%i==0)  
24:                    return 0;  
25:        }  
26:        return 1;  
27:  }  

1 comment: