Write a C program to Check whether a given number is a prime or not using function
Program:-
#include<stdio.h>
int is_prime_number(int n)
{
int i;
for(i = 2; i <= n/2; i++)
{
if(n % i ==0)
{
break;
}
}
if(i > n/2)
printf(“\n%d is a Prime Number\n”,n);
else
printf(“\n%d is not a Prime Number\n”, n);
}
int main()
{
int n,i;
printf(“\nEnter the number : “);
scanf(“%d”,&n);
is_prime_number(n);
return 0;
}
int is_prime_number(int n)
{
int i;
for(i = 2; i <= n/2; i++)
{
if(n % i ==0)
{
break;
}
}
if(i > n/2)
printf(“\n%d is a Prime Number\n”,n);
else
printf(“\n%d is not a Prime Number\n”, n);
}
int main()
{
int n,i;
printf(“\nEnter the number : “);
scanf(“%d”,&n);
is_prime_number(n);
return 0;
}
Output:-
....
0 comments:
Post a Comment