the following code is to print the nearest twin primes to a number.for eg if the input is 20...the output should be 17 and 19
logic to my code is that it checks for twin primes within the range of a given number and then adds the twin primes.....the twin primes with the largest sum are printed and thus are the nearest primes
but there is some logical error in the program
if i input 20 the output is 132435 and continues till 21...pls help
class nearesttwinprime
{
public void main(int num)
{
int i,j,a,k,b,sum=0,max=0;
for(i=1;i<=num;i++)
{
j=i;
k=i+2;
a=prime(j);
b=prime(k);
if(a==1&&b==1)
{
sum=j+k;
if(max<sum)
sum=max;
}
System.out.print(j);
System.out.print(k);
}}
public int prime(int newnum)
{
int l,c=0;
for(l=1;l<=newnum;l++)
{
if(newnum%l==0)
{
c++;
}
}
if(c==2)
return 1;
else
return 0;
}
}