Count the positive, negative and zero values from an Array elements using C Programming Language

Published: 24 July 2024
on channel: TrustCoding
243
1

C program to count Positive , Negative and zero numbers in an Array
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:

see the video

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)

Regards,
Md. Ahsan Arif
University of Scholars

#cprogramming #cprogrammingtutorial #cprogrammingtutorialforbeginners
#videotutorial #ius #universityofscholars #banglatutorial #bangladesh #bangla


On this page of the site you can watch the video online Count the positive, negative and zero values from an Array elements using C Programming Language with a duration of hours minute second in good quality, which was uploaded by the user TrustCoding 24 July 2024, share the link with friends and acquaintances, this video has already been watched 243 times on youtube and it was liked by 1 viewers. Enjoy your viewing!