0% found this document useful (0 votes)
2 views

Logic to count negative

The document outlines a C program that counts the number of negative elements in an array. It provides a step-by-step logic for inputting the array size and elements, initializing a count variable, and iterating through the array to increment the count for each negative element. Finally, it prints the total count of negative elements in the array.

Uploaded by

mrwhiza
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Logic to count negative

The document outlines a C program that counts the number of negative elements in an array. It provides a step-by-step logic for inputting the array size and elements, initializing a count variable, and iterating through the array to increment the count for each negative element. Finally, it prints the total count of negative elements in the array.

Uploaded by

mrwhiza
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

Logic to count negative/positive elements in array

In previous post we learned to print negative elements in array. Here for this problem
we will use same logic, but instead of printing negative elements we will count them.

Step by step descriptive logic to count negative elements in array.

i. Input size and array elements from user. Store it in some variable
say size and arr.
ii. Declare and initialize a variable count with zero, to store count of negative
elements.
iii. Iterate through each element in array, run a loop from 0 to size. Loop structure
should look like for(i=0; i<size; i++).
iv. Inside loop check if current number is negative, then increment count by 1.
v. Finally, after loop you are left with total negative element count.

Program to count negative elements in array


/**
* C program to count total number of negative elements in array
*/

#include <stdio.h>

#define MAX_SIZE 100 // Maximum array size

int main()
{
int arr[MAX_SIZE]; // Declares array of size 100
int i, size, count = 0;

/* Input size of array */


printf("Enter size of the array : ");
scanf("%d", &size);

/* Input array elements */


printf("Enter elements in array : ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

/*
* Count total negative elements in array
*/
for(i=0; i<size; i++)
{
/* Increment count if current array element is negative
*/
if(arr[i] < 0)
{
count++;
}
}

printf("\nTotal negative elements in array = %d", count);

return 0;
}

You might also like