Code:
Output:
/* Target by 2 Sum */
import java.util.Scanner;
class Target
{
public static void main( String args[] )
{
Scanner scan = new Scanner(System.in);
System.out.print("\n\n Enter number of array elements: ");
int n = scan.nextInt();
int a[] = new int[n];
int i , j ,num ;
System.out.println("\n\n Enter "+ n +" array elements: ");
for(i=0;i<n;i++)
{
a[i] = scan.nextInt();
}
// Printing array elements
System.out.print("\n\n Array Elements: ");
for(i=0;i<n;i++)
{
System.out.println(" " + a[i]);
}
System.out.print("\n\n Enter any number for sum= ");
num = scan.nextInt();
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if( (a[i]+a[j]) == num )
{
System.out.println(" Two numbers are = "+a[i]+" "+a[j] );
break;
}
}
}
}
}
import java.util.Scanner;
class Target
{
public static void main( String args[] )
{
Scanner scan = new Scanner(System.in);
System.out.print("\n\n Enter number of array elements: ");
int n = scan.nextInt();
int a[] = new int[n];
int i , j ,num ;
System.out.println("\n\n Enter "+ n +" array elements: ");
for(i=0;i<n;i++)
{
a[i] = scan.nextInt();
}
// Printing array elements
System.out.print("\n\n Array Elements: ");
for(i=0;i<n;i++)
{
System.out.println(" " + a[i]);
}
System.out.print("\n\n Enter any number for sum= ");
num = scan.nextInt();
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if( (a[i]+a[j]) == num )
{
System.out.println(" Two numbers are = "+a[i]+" "+a[j] );
break;
}
}
}
}
}
Output: