Example 1 : C Program to find prime number using user defined function :-
#include<stdio.h>
#include<conio.h>
int primeNumber(int); /* function declaration or prototype, int arg int returns */
void main()
{
int num,prime;
clrscr();
printf("Enter an integer Number :");
scanf("%d",&num);
prime = primeNumber(num); /* function call here with Actual Parameter num, rtns integer value and assigning to the var prime */
if(prime==1)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
getch();
}
int primeNumber(int n) /* function definition with Formal Parameter n and returns an integer */
{
int i;
for( i=2 ; i<=n/2 ; i++)
{
if(n%i==0)
return 0;
}
return 1;
}
/* prime number 3,7,11,17.... */
Example 2 : C Program to find prime numbers upto a limit using user defined function :-
#include<stdio.h>
#include<conio.h>
int primeNumber(int);
void main()
{
int num,lmt;
clrscr();
printf("Enter a limit :");
scanf("%d",&lmt);
for(num=3 ; num <= lmt ; num++)
{
if(primeNumber(num)==1)
{
printf("\n%d",num);
}
}
getch();
}
int primeNumber(int n)
{
int i;
for( i=2 ; i<=n/2 ; i++)
{
if(n%i==0)
return 0;
}
return 1;
}