C Program to Count Positive and Negative Numbers in an Array

In this tutorial, i am going to show you how to count positive and negative numbers in an array with the help of for loop, while loop, and function in c programs.

All C Programs to Count Positive and Negative Numbers in an Array

  • C Program to Count Positive and Negative Numbers in an Array using For Loop
  • C Program to Count Positive and Negative Numbers in an Array using While Loop
  • C Program to Count Positive and Negative Numbers in an Array using Function

C Program to Count Positive and Negative Numbers in an Array using For Loop

/* C Program to Count Positive and Negative Numbers in an Array */
#include<stdio.h>
 
int main()
{
 int Size, i, a[10];
 int Positive_Count = 0, Negative_Count = 0;
 
 printf("\n Please Enter the Size of an Array :  ");
 scanf("%d", &Size);
 
 printf("\nPlease Enter the Array Elements\n");
 for(i = 0; i < Size; i++)
 {
      scanf("%d", &a[i]);
 }
  
 for(i = 0; i < Size; i ++)
 {
   if(a[i] >= 0)
   {
 	Positive_Count++;
   }
   else
   {
 	Negative_Count++;
   }
 }
  
 printf("\n Total Number of Positive Numbers in this Array = %d ", Positive_Count);
 printf("\n Total Number of Negative Numbers in this Array = %d ", Negative_Count);
 return 0;
}

The result of the above c program; as follows:

Please Enter the Size of an Array :  5
Please Enter the Array Elements
1 2 -3 4 -5
Total Number of Positive Numbers in this Array = 3 
 Total Number of Negative Numbers in this Array = 2 

C Program to Count Positive and Negative Numbers in an Array using While Loop

/* C Program to Count Positive and Negative Numbers in an Array */
#include<stdio.h>
 
int main()
{
 int Size, i, j = 0, a[10];
 int Positive_Count = 0, Negative_Count = 0;
 
 printf("\n Please Enter the Size of an Array :  ");
 scanf("%d", &Size);
 
 printf("\nPlease Enter the Array Elements\n");
 for(i = 0; i < Size; i++)
 {
      scanf("%d", &a[i]);
 }
  
 while(j < Size)
 {
   if(a[j] >= 0)
   {
 	Positive_Count++;
   }
   else
   {
 	Negative_Count++;
   }
   j++;
 }
  
 printf("\n Total Number of Positive Numbers in this Array = %d ", Positive_Count);
 printf("\n Total Number of Negative Numbers in this Array = %d ", Negative_Count);
 return 0;
}

The result of the above c program; as follows:

Please Enter the Size of an Array :  5
Please Enter the Array Elements
1 2 -3 4 -5
Total Number of Positive Numbers in this Array = 3 
 Total Number of Negative Numbers in this Array = 2 

C Program to Count Positive and Negative Numbers in an Array using Function

/* C Program to Count Positive and Negative Numbers in an Array */
#include<stdio.h>
int CountPositiveNumbers(int a[], int Size);
int CountNegativeNumbers(int a[], int Size);
int main()
{
 int Size, i, a[10];
 int Positive_Count = 0, Negative_Count = 0;
 
 printf("\n Please Enter the Size of an Array  :  ");
 scanf("%d", &Size);
 
 printf("\nPlease Enter the Array Elements :  ");
 for(i = 0; i < Size; i++)
 {
      scanf("%d", &a[i]);
 }
 
 Positive_Count = CountPositiveNumbers(a, Size);
 Negative_Count = CountNegativeNumbers(a, Size);
  
 printf("\n Total Number of Positive Numbers in this Array = %d ", Positive_Count);
 printf("\n Total Number of Negative Numbers in this Array = %d ", Negative_Count);
 return 0;
}
int CountPositiveNumbers(int a[], int Size)
{
  int i, Positive_Count = 0;
  printf("\n List of Positive Numbers in this Array: ");
  
  for(i = 0; i < Size; i ++)
  {
     if(a[i] >= 0)
     {
 	printf("%d  ", a[i]);
 	Positive_Count++;
     }
   }
   return Positive_Count;
}
int CountNegativeNumbers(int a[], int Size)
{
  int i, Negative_Count = 0;
  printf("\n List of Negative Numbers in this Array: ");
  for(i = 0; i < Size; i ++)
  {
     if(a[i] < 0)
     {
 	printf("%d  ", a[i]);
 	Negative_Count++;
     }
   }
   return Negative_Count;
}

The result of the above c program; as follows:

Please Enter the Size of an Array :  5
Please Enter the Array Elements
1 2 -3 4 -5
Total Number of Positive Numbers in this Array = 3 
 Total Number of Negative Numbers in this Array = 2 

More C Programming Tutorials

Leave a Comment