import java.util.*;
public class qstn_1
{
public static int check(int n,int pos,int input)
{
int result=0;
int prime[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47};
for(int i = pos;i<prime.length;i++)
{
int sum = n+prime[i];
if(sum==input)
{
result= prime[i];
break;
}
sum = 0;
}
return result;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
// boundary conditions
if(n<9 || n>50)
{
System.out.println("INVALID INPUT. NUMBER OUT OF RANGE.");
System.exit(0);
}
if(n>1 && n%2!=0)
{
System.out.println("INVALID INPUT. NUMBER IS ODD.");
System.exit(0);
}
int prime[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47};
int n1[] = new int[10];
int n2[] = new int[10];
int ins1 = 0,ins2 = 0;
int length = prime.length;
int pos = 0;
int result=0;
int pos2 = 0;
while(length!=0)
{
pos2 = pos;
result = check(prime[pos],pos++,n);
if(result!=0)
{
n1[ins1++] = prime[pos2];// number sending
n2[ins2++] = result; // result from function
}
length--;
}
for(int i=0;i<ins1;i++)
{
System.out.println(n1[i]+", "+n2[i]);
}
}
}
No comments:
Post a Comment