class NearestPrime
{
public void n_prime(int n)
{
int up,dn,i;
up=n+1;
while(true)
{
i=isprime(up);
if(i==0)
break;
up++;
}
dn=n-1;
while(true)
{
i=isprime(dn);
if(i==0)
break;
dn--;
}
System.out.print("the nearest prime number is");
if((n-dn)<(up-n))
System.out.println(dn);
else if((up-n)<(n-dn))
System.out.println(up);
else
System.out.println(up+" "+dn);
}
public int isprime(int a)
{ int f=0;
for(int j=2;j<=a/2;j++)
{
if (a%j==0)
{
f=1;break;
}}
return f;
}
}
{
public void n_prime(int n)
{
int up,dn,i;
up=n+1;
while(true)
{
i=isprime(up);
if(i==0)
break;
up++;
}
dn=n-1;
while(true)
{
i=isprime(dn);
if(i==0)
break;
dn--;
}
System.out.print("the nearest prime number is");
if((n-dn)<(up-n))
System.out.println(dn);
else if((up-n)<(n-dn))
System.out.println(up);
else
System.out.println(up+" "+dn);
}
public int isprime(int a)
{ int f=0;
for(int j=2;j<=a/2;j++)
{
if (a%j==0)
{
f=1;break;
}}
return f;
}
}
No comments:
Post a Comment