Write a java program to Define a method to Display pair of elements which sum is Equal to n
import java.util.Scanner;
public class PairInArray {
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Enter Length of Array : ");
int n=sc.nextInt();
int arr[]= new int[n];
System.out.println("enter value in array : ");
for (int i=0; i<n; i++) {
arr[i]=sc.nextInt();
}
System.out.println("\nEnter a number you check pair : ");
int pair=sc.nextInt();
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length; j++) {
if(pair==arr[i]+arr[j])
System.out.println(i+" "+j);
}
}
}
}
Comments
Post a Comment