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

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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