Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Code to count Positive, Negative and zero in an array of C

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

Posted on November 4, 2020November 4, 2020

Table of Contents

  • Code to count Positive, Negative and zero in an array of C
    • Count Positive, Negative and zero in an array of C
      • Count Positive, Negative and zero in an array of C using for loop
      • Count Positive, Negative and zero in an array of C using while loop
      • Count Positive, Negative and zero in an array of C using do-while loop
      • Count Positive, Negative and zero in an array of C using function
    • Related

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

 

Related

Recent Posts

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes