Program to Count Positive, Negative and Zeros in an Array

Program to Count Positive, Negative and Zeros in an Array

  • Write a program to Count Positive, Negative and Zeros in an Array in C
  • Write a program to Count Positive, Negative and Zeros in an Array in C++
  • Write a program to Count Positive, Negative and Zeros in an Array in Python
  • Write a program to Count Positive, Negative and Zeros in an Array in PHP
  • Write a program to Count Positive, Negative and Zeros in an Array in Java
  • Write a program to Count Positive, Negative and Zeros in an Array in Java Script
  • Write a program to Count Positive, Negative and Zeros in an Array in C#

Explanation:

You can iterate through an array and use counters to keep track of each category in order to count the positive, negative, and zero elements.

Logic:

  1. Initialize Counters:
    • positive_count = 0
    • negative_count = 0
    • zero_count = 0
  2. Iterate Through the Array:
    • For each element:
      • If the element is greater than 0, increment positive_count
      • If the element is less than 0, increment negative_count
      • If the element is equal to 0, increment zero_count
  3. Output:
    • After processing all elements, return or print the values of the three counters.

Program to Count Positive, Negative and Zeros in an Array

#include <stdio.h>

int main() {
    int arr[] = {10, -5, 0, 45, -20, 0};  // Example array
    int size = sizeof(arr) / sizeof(arr[0]);
    int positive = 0, negative = 0, zero = 0;

    for (int i = 0; i < size; i++) {
        if (arr[i] > 0) {
            positive++;
        } else if (arr[i] < 0) {
            negative++;
        } else {
            zero++;
        }
    }

    printf("Positive: %d\n", positive);
    printf("Negative: %d\n", negative);
    printf("Zeros: %d\n", zero);

    return 0;
}

#include <iostream>
using namespace std;

int main() {
    int arr[] = {10, -5, 0, 45, -20, 0};  // Example array
    int size = sizeof(arr) / sizeof(arr[0]);
    int positive = 0, negative = 0, zero = 0;

    for (int i = 0; i < size; i++) {
        if (arr[i] > 0) {
            positive++;
        } else if (arr[i] < 0) {
            negative++;
        } else {
            zero++;
        }
    }

    cout << "Positive: " << positive << endl;
    cout << "Negative: " << negative << endl;
    cout << "Zeros: " << zero << endl;

    return 0;
}

arr = [10, -5, 0, 45, -20, 0]  # Example array

positive = sum(1 for x in arr if x > 0)
negative = sum(1 for x in arr if x < 0)
zero = sum(1 for x in arr if x == 0)

print(f"Positive: {positive}")
print(f"Negative: {negative}")
print(f"Zeros: {zero}")

<?php
$arr = array(10, -5, 0, 45, -20, 0);  // Example array

$positive = 0;
$negative = 0;
$zero = 0;

foreach ($arr as $num) {
    if ($num > 0) {
        $positive++;
    } elseif ($num < 0) {
        $negative++;
    } else {
        $zero++;
    }
}

echo "Positive: $positive\n";
echo "Negative: $negative\n";
echo "Zeros: $zero\n";
?>

public class Main {
    public static void main(String[] args) {
        int[] arr = {10, -5, 0, 45, -20, 0};  // Example array
        int positive = 0, negative = 0, zero = 0;

        for (int num : arr) {
            if (num > 0) {
                positive++;
            } else if (num < 0) {
                negative++;
            } else {
                zero++;
            }
        }

        System.out.println("Positive: " + positive);
        System.out.println("Negative: " + negative);
        System.out.println("Zeros: " + zero);
    }
}

const arr = [10, -5, 0, 45, -20, 0];  // Example array

let positive = 0, negative = 0, zero = 0;

arr.forEach(num => {
    if (num > 0) {
        positive++;
    } else if (num < 0) {
        negative++;
    } else {
        zero++;
    }
});

console.log(`Positive: ${positive}`);
console.log(`Negative: ${negative}`);
console.log(`Zeros: ${zero}`);

using System;

class Program {
    static void Main() {
        int[] arr = {10, -5, 0, 45, -20, 0};  // Example array
        int positive = 0, negative = 0, zero = 0;

        foreach (int num in arr) {
            if (num > 0) {
                positive++;
            } else if (num < 0) {
                negative++;
            } else {
                zero++;
            }
        }

        Console.WriteLine($"Positive: {positive}");
        Console.WriteLine($"Negative: {negative}");
        Console.WriteLine($"Zeros: {zero}");
    }
}

List of All Programs