//Written by:MA saleh
//find prime using function
#include<stdio.h>
#include<math.h>
int prime(int n)
{
int i, z;
if(n < 2)
{
return 0;
}
if (n == 2)
{
return 1;
}
if(n%2== 0)
{
return 0;
}
z = sqrt(n); //get root of this number
for(i = 3; i<= z; i= i+2)
{
if(n%i == 0)
{
return 0;
}
}
return 1;
}
int main()
{
int n;
printf("Enter a number for checking prime: ");
while(scanf("%d", &n) == 1)
{
int is_prime = prime(n);
if(is_prime == 1)
{
printf("The number is prime\n");
}
else
{
printf("The number is not prime\n");
}
}
return 0;
//Copyright@MAS
}
//find prime using function
#include<stdio.h>
#include<math.h>
int prime(int n)
{
int i, z;
if(n < 2)
{
return 0;
}
if (n == 2)
{
return 1;
}
if(n%2== 0)
{
return 0;
}
z = sqrt(n); //get root of this number
for(i = 3; i<= z; i= i+2)
{
if(n%i == 0)
{
return 0;
}
}
return 1;
}
int main()
{
int n;
printf("Enter a number for checking prime: ");
while(scanf("%d", &n) == 1)
{
int is_prime = prime(n);
if(is_prime == 1)
{
printf("The number is prime\n");
}
else
{
printf("The number is not prime\n");
}
}
return 0;
//Copyright@MAS
}
0 comments:
Post a Comment