-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTargetNumber.java
More file actions
40 lines (38 loc) · 772 Bytes
/
Copy pathTargetNumber.java
File metadata and controls
40 lines (38 loc) · 772 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.Scanner;
public class TargetNumber
{
public static void main(String[] args)
{
int i,j,size,target,index1,index2;
System.out.println("Enter the size of the array");
Scanner s=new Scanner(System.in);
size=s.nextInt();
System.out.println("Only "+size+" elements ar allow");
int a[]=new int[size];
for(i=0;i<size;i++)
{
a[i]=s.nextInt();
}
System.out.print("The elements are:");
for(i=0;i<size;i++)
{
System.out.print(a[i]+" ");
}
//Now finding the target elements:
System.out.print("\nEnter the target elements:");
target=s.nextInt();
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(target==a[i] + a[j])
{
index1=i;
index2=j;
System.out.println("The numbers are:"+"["+a[i]+" ,"+a[j]+"]");
System.out.println("Index: "+"["+index1+","+index2+"]");
}
}
}
}
}