C Program to Count Positive and Negative Numbers in an Array

C Program to Count Positive and Negative Numbers in an Array

C program to count positive and negative numbers in an array; Through this tutorial, we will learn how to count positive and negative numbers in an array using for loop, while loop, and function in c programs.

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

To count positive and negative numbers in an array using for loop, while loop, and function in c:

  • 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 output 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 output 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 output 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 

Recommended C Programs

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *