Don't miss your Year-End Offer: Up to 20% OFF

Find Negative Numbers in Array

11 February 2024
public class HackerAlgo {

    public static void printNegativeNumbers(int[] arr) {
        for (int num : arr) {
            if (num< 0) {
System.out.print(num + " ");
            }
        }
System.out.println();
    }

    public static void main(String[] args) {
int[] exampleArr = {1, 5, -3, 2, 0, -8, 4};
printNegativeNumbers(exampleArr);
    }
}
def print_negative_numbers(arr):
    negatives = [num for num in arr if num< 0]
    print(negatives)

# Example usage
example_arr = [1, 5, -3, 2, 0, -8, 4]
print_negative_numbers(example_arr)
#include <iostream>
#include <vector>

class HackerAlgo {
public:
    static void printNegativeNumbers(const std::vector<int>&arr) {
        for (int num : arr) {
            if (num< 0) {
std::cout<<num<< " ";
            }
        }
std::cout<< std::endl;
    }
};

int main() {
    // Example usage
std::vector<int>exampleArr = {1, 5, -3, 2, 0, -8, 4};
HackerAlgo::printNegativeNumbers(exampleArr);

    return 0;
}
class HackerAlgo {
    static printNegativeNumbers(arr) {
const negatives = arr.filter(num =>num< 0);
        console.log(negatives);
    }
}

// Example usage
constexampleArr = [1, 5, -3, 2, 0, -8, 4];
HackerAlgo.printNegativeNumbers(exampleArr);

Leave a Comment