Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Ex4 {
- // Write a Java program to find the sum of the two elements of a given array
- // which is equal to a given integer.
- // Sample array: [8,1,2,4,5,6]
- // Target value: 6.
- public static void main(String[] args) {
- int[] arr = { 8, 1, 2, 4, 5, 6 };
- int sum = 6;
- boolean found = false;
- for (int i = 0; i < arr.length - 1; i += 1) {
- for (int j = i + 1; j < arr.length; j += 1) {
- if (arr[i] + arr[j] == sum) {
- System.out.println(arr[i] + "+" + arr[j] + " = " + sum);
- found = true;
- // return;
- }
- }
- }
- if (found == false)
- System.out.println("There is no two numbers with sum of " + sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement