Check Whether An Array Contains An Specific Number | Java Programs

Output:

*** Check Whether An Array Contain An Specific Number ***

Array Elements Are: [1, 2, 3, 4, 5]
Array Contains 2?
Answer: true
Array Contains 200?
Answer: false

Click Here For Java Online Compiler

Solution:

import java.util.Arrays;
public class ArraySpecificValue {

    public static boolean contains(int[] arr, int item) {
        for (int n : arr) {
            if (item == n) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        System.out.println("*** Check Whether An Array Contain An Specific Number ***" + "\n");
        System.out.println("Array Elements Are: " + Arrays.toString(array));
        System.out.println("Array Contains 2?");
        System.out.println("Answer: " + contains(array, 2));
        System.out.println("Array Contains 200?");
        System.out.println("Answer: " + contains(array, 200));
    }
}
Share This :

Related Post



sentiment_satisfied Emoticon