C program to count Positive and Negative numbers in an Array (original) (raw)
Last Updated : 12 Sep, 2022
Given an array arr of integers of size N, the task is to find the count of positive numbers and negative numbers in the array Examples:
Input: arr[] = {2, -1, 5, 6, 0, -3} Output: Positive elements = 3 Negative elements = 2 There are 3 positive, 2 negative, and 1 zero. Input: arr[] = {4, 0, -2, -9, -7, 1} Output: Positive elements = 2 Negative elements = 3 There are 2 positive, 3 negative, and 1 zero.
Approach:
- Traverse the elements in the array one by one.
- For each element, check if the element is less than 0. If it is, then increment the count of negative elements.
- For each element, check if the element is greater than 0. If it is, then increment the count of positive elements.
- Print the count of negative and positive elements.
Below is the implementation of the above approach:
C `
// C program to find the count of positive // and negative integers in an array
#include <stdio.h>
// Function to find the count of // positive integers in an array int countPositiveNumbers(int* arr, int n) { int pos_count = 0; int i; for (i = 0; i < n; i++) { if (arr[i] > 0) pos_count++; } return pos_count; }
// Function to find the count of // negative integers in an array int countNegativeNumbers(int* arr, int n) { int neg_count = 0; int i; for (i = 0; i < n; i++) { if (arr[i] < 0) neg_count++; } return neg_count; }
// Function to print the array void printArray(int* arr, int n) { int i;
printf("Array: "); for (i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); }
// Driver program int main() { int arr[] = { 2, -1, 5, 6, 0, -3 }; int n; n = sizeof(arr) / sizeof(arr[0]);
printArray(arr, n);
printf("Count of Positive elements = %d\n", countPositiveNumbers(arr, n)); printf("Count of Negative elements = %d\n", countNegativeNumbers(arr, n));
return 0; }
`
Output:
Array: 2 -1 5 6 0 -3 Count of Positive elements = 3 Count of Negative elements = 2
Time complexity: O(n) where n is size of the given array
Auxiliary space: O(1)
Similar Reads
- C Program to Rearrange positive and negative numbers in O(n) time and O(1) extra space An array contains both positive and negative numbers in random order. Rearrange the array elements so that positive and negative numbers are placed alternatively. Number of positive and negative numbers need not be equal. If there are more positive numbers they appear at the end of the array. If the 3 min read
- C Program to Check Whether a Number is Positive or Negative or Zero Write a C program to check whether a given number is positive, negative, or zero.ExamplesInput: 10Output: PositiveExplanation: Since 10 is greater than 0, it is positive.Input: -5Output: NegativeExplanation: Since -5 is less than 0, it is negative.Different Ways to Check for Positive Numbers, Negati 3 min read
- Program to find absolute value of a given number Given an integer N, The task is to find the absolute value of the given integer. Examples: Input: N = -6 Output: 6Explanation: The absolute value of -6 is 6 which is non negative Input: N = 12 Output: 12 Naive Approach: To solve the problem follow the below idea: The absolute value of any number is 9 min read
- C program to count frequency of each element in an array Given an array arr[] of size N, the task is to find the frequency of each distinct element present in the given array. Examples: Input: arr[] = { 1, 100000000, 3, 100000000, 3 } Output: { 1 : 1, 3 : 2, 100000000 : 2 } Explanation: Distinct elements of the given array are { 1, 100000000, 3 } Frequenc 4 min read
- How to Find the Range of Numbers in an Array in C? The range of numbers within an array is defined as the difference between the maximum and the minimum element present in the array. In this article, we will learn how we can find the range of numbers in an array in C. Example Input:int arr[] = { 23, 12, 45, 20, 90, 89, 95, 32, 65, 19 }Output: The ra 2 min read
- How to Find the Mode of Numbers in a Sorted Array in C? The mode of the given numbers can be defined as the value that occurs the most in the given dataset or the value with the highest frequency. In this article, we will learn how to find the mode of all elements in a sorted array of integers in C. Example: Input:myArray = {1, 2, 3, 3, 5, 5, 5, 5, 6, 7} 2 min read
- How to Convert an Integer to a String in C? In C, integers can be represented as strings with each digit represented by corresponding numeric character. In this article, we will learn how to convert integers into the stringExamplesInput: 1234Output: "1234"Explanation: The integer 1234 is converted to the string "1234".Input: -567Output: "-567 3 min read
- C Program to count the number of zeros from 0 to N Given a number N, the task is to write C program to count the number of zeros from 0 to N. Examples: Input: N = 10 Output: 2 Explanation: The number with zeros are 0, 10 till 10. Hence the count of zeros is 2. Input: N = 20 Output: 3 Explanation: The number with zeros are 0, 10, 20 till 20. Hence th 2 min read
- C program to input an array from a sequence of space-separated integers Given a string S consisting of space-separated integers, the task is to write a C program to take the integers as input from the string S and store them in an array arr[]. Examples: Input: S = "1 2 3 4"Output: {1, 2, 3, 4} Input: S = "32 12"Output: {32, 12} Approach: The idea is to solve the given p 2 min read
- C Program To Find Next Greater Element Given an array, print the Next Greater Element (NGE) for every element. The Next greater Element for an element x is the first greater element on the right side of x in the array. Elements for which no greater element exist, consider the next greater element as -1. Examples: For an array, the righ 5 min read