Wednesday, 4 July 2018

Finding the possibility of Adding two numbers in an given array and finding the number which we give at runtime in java

package learning;

import java.util.Scanner;

public class FindingNoOfPossibilitiesOfAddingTwoNumbersInAnArray {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int s[] = { 1, 2, 3, 4, 5 };
        int a = s.length;
        System.out.println("Enter your number");

        int b = sc.nextInt();

        for (int i = 0; i < a; i++) {

            if (s[i] < a) {

                for (int x = a; x > i; x--) {
                    if (s[i] + s[x - 1] == b) {
                        System.out.println("s[i] is :"+s[i]);
                        System.out.println("s[x-1] is :"+s[x-1]);
                        System.out.println("The possible sum of two numbers is "+(s[i] + s[x - 1]));
                    }
                }
            }

        }

    }

}

No comments:

Post a Comment

If you Like my blog Spread it and help friends for whom this blog is useful for their career.