C Language

Code to count Positive, Negative and zero in an array of C

Code to count Positive, Negative and zero in an array of C

In this article, we will discuss the concept of Code to count Positive, Negative numbers and zero in an array of C language

In this program, we are going to learn how to count the Positive numbers, Negative numbers and zeros in an array using several ways in C language.

This is done using for loop , while loop, do-while loop and function in C language

Count Positive, Negative and zero in an array of C

Count Positive, Negative and zero in an array of C using for loop

In this program, we will count positive numbers,negative numbers and zeros in an array using a for loop in C language

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,size,countPos=0, countNeg=0, countZero=0,arr[100];
    printf("Enter the array size\n");
    scanf("%d",&size);
    printf("Enter %d elements\n",size);
    for(i=0; i<size; i++){
     scanf("%d",&arr[i]);
    }
    for(i=0; i<size; i++){
     if(arr[i]<0){
        countNeg++;
     }
     else if(arr[i]>0){
        countPos++;
     }
     else{
        countZero++;
     }
    }
    printf("Positive numbers are %d\n",countPos);
    printf("Negative numbers are %d\n",countNeg);
    printf("zeros are %d",countZero);
    getch();
    return 0;
}

When the above code is executed it produces the following result

Enter the array size
10
enter 10 elements
45
0
-73
67
23
-87
-7
24
52
56

Positive numbers are 6
Negative numbers are 3
Zeros are 1

 

 

Count Positive, Negative and zero in an array of C using while loop

In this program, we will count positive numbers,negative numbers and zeros in an array using a while loop in C language

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,size,countPos=0, countNeg=0, countZero=0,arr[100];
    printf("Enter the array size\n");
    scanf("%d",&size);
    printf("Enter %d elements\n",size);
    i=0;
    while( i<size){
     scanf("%d",&arr[i]);
      i++;
    }
    i=0;
    while( i<size){
     if(arr[i]<0){
        countNeg++;
     }
     else if(arr[i]>0){
        countPos++;
     }
     else{
        countZero++;
     }
      i++;
    }
    printf("Positive numbers are %d\n",countPos);
    printf("Negative numbers are %d\n",countNeg);
    printf("zeros are %d",countZero);
    getch();
    return 0;
}

When the above code is executed it produces the following result

Enter the array size
12
Enter 10 elements

45
-67
0
64
-54
-34
45
23
62
72
68
-65

Positive numbers are 7
Negative numbers are 4
Zeros are 1

Count Positive, Negative and zero in an array of C using do-while loop

In this program, we will count positive numbers,negative numbers and zeros in an array using a do-while loop in C language

Program 3

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,size,countPos=0, countNeg=0, countZero=0,arr[100];
    printf("Enter the array size\n");
    scanf("%d",&size);
    printf("Enter %d elements\n",size);
    i=0;
   do{
     scanf("%d",&arr[i]);
      i++;
    } while( i<size);
    i=0;
    do{
     if(arr[i]<0){
        countNeg++;
     }
     else if(arr[i]>0){
        countPos++;
     }
     else{
        countZero++;
     }
      i++;
    }while( i<size);
    printf("Positive numbers are %d\n",countPos);
    printf("Negative numbers are %d\n",countNeg);
    printf("zeros are %d",countZero);
    getch();
    return 0;
}


When the above code is executed it produces the following result

Enter the array size
8
Enter 8 elements
78
-98
0
45
67
83
29

Positive numbers are 5
Negative numbers are 2
Zeros are 1


Count Positive, Negative and zero in an array of C using function

In this program, we will count positive numbers,negative numbers and zeros in an array using a function in C language

Program 4

#include <stdio.h>
#include <stdlib.h>
int countPositive(int arr[],int size);
int countNegative(int arr[],int size);
int countZero(int arr[],int size);
int main()
{
    int size,i,arr[10];
    int posCount=0,negCount=0,zeroCount=0;
    printf("Please enter the size of an array \n");
    scanf("%d",&size);
      printf("Please enter the array elements \n");
    for(i=0; i<size; i++){
        scanf("%d",&arr[i]);
    }
    printf("Total positive numbers in this array= %d\n",countPositive(arr,size));
    printf("Total Negative numbers in this array= %d\n",countNegative(arr,size));
    printf("\nTotal zeros in this array= %d\n",countZero(arr,size));
    getch();
    return 0;
}

int countPositive(int arr[],int size){
int i, posCount=0;
printf("\nList of positive number in this array: \n");
 for(i=0; i<size; i++){
        if(arr[i]>0){
            printf("%d\n",arr[i]);
            posCount++;
        }
    }
    return posCount;

}

int countNegative(int arr[],int size){
int i, negCount=0;
printf("\nList of negative number in this array: \n");
 for(i=0; i<size; i++){
        if(arr[i]<0){
            printf("%d\n",arr[i]);
            negCount++;
        }
    }
    return negCount;

}

int countZero(int arr[],int size){
int i, zeroCount=0;

 for(i=0; i<size; i++){
        if(arr[i]==0){
            zeroCount++;
        }
    }
    return zeroCount;

}

When the above code is executed it produces the following result

 

Suggested for you

Operator in C language

For loop in C language

while loop in C language

Do-while loop in C language

function in C language

 

Similar post

Program to count Positive, Negative and zero in an array of C++

Program to count Positive, Negative and zero in an array of Python

Program to count Positive, Negative and zero in an array of Java

Program to count Positive, Negative and zero in an array of C

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.