C program to put positive and negative elements or numbers of the array into two separate arrays; Through this tutorial, we will learn how to put positive and negative elements or numbers of an array into two separate arrays using standard method and function in c programs.
Programs To Put Positive and Negative Numbers in two Separate Arrays in C
- C Program To Put Positive and Negative Numbers in two Separate Arrays using For Loop
- C Program To Put Positive and Negative Numbers in two Separate Arrays using While Loop
- C Program To Put Even And Odd Elements Of Array Into Two Separate Arrays using Function
C Program To Put Positive and Negative Numbers in two Separate Arrays using For Loop
#include<stdio.h>
void PrintArray(int a[], int Size);
int main()
{
int Size, i, a[10], Positive[10], Negative[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]);
}
for(i = 0; i < Size; i ++)
{
if(a[i] >= 0)
{
Positive[Positive_Count] = a[i];
Positive_Count++;
}
else
{
Negative[Negative_Count] = a[i];
Negative_Count++;
}
}
printf("\n Total Number of Positive Numbers in this Array = %d ", Positive_Count);
printf("\n Array Elements in Positive Array : ");
PrintArray(Positive, Positive_Count);
printf("\n Total Number of Negative Numbers in this Array = %d ", Negative_Count);
printf("\n Array Elements in Negative Array : ");
PrintArray(Negative, Negative_Count);
return 0;
}
void PrintArray(int a[], int Size)
{
int i;
for(i = 0; i < Size; i++)
{
printf("%d \t ", a[i]);
}
printf("\n");
}
The output of the above c program; is as follows:
Please Enter the Size of an Array : 5 Please Enter the Array Elements : 1 2 3 -5 -4 Total Number of Positive Numbers in this Array = 3 Array Elements in Positive Array : 1 2 3 Total Number of Negative Numbers in this Array = 2 Array Elements in Negative Array : -5 -4
C Program To Put Positive and Negative Numbers in two Separate Arrays using While Loop
#include<stdio.h>
void PrintArray(int a[], int Size);
int main()
{
int Size, i, j = 0, a[10], Positive[10], Negative[10];
int Positive_Count = 0, Negative_Count = 0;
printf("\n Please Enter the Size of an Array : ");
scanf("%d", &Size);
printf("\n Please Enter the Array Elements : ");
for(i = 0; i < Size; i++)
{
scanf("%d", &a[i]);
}
while(j < Size)
{
if(a[j] >= 0)
{
Positive[Positive_Count] = a[j];
Positive_Count++;
}
else
{
Negative[Negative_Count] = a[j];
Negative_Count++;
}
j++;
}
printf("\n Total Number of Positive Numbers in this Array = %d ", Positive_Count);
printf("\n Array Elements in Positive Array : ");
PrintArray(Positive, Positive_Count);
printf("\n Total Number of Negative Numbers in this Array = %d ", Negative_Count);
printf("\n Array Elements in Negative Array : ");
PrintArray(Negative, Negative_Count);
return 0;
}
void PrintArray(int a[], int Size)
{
int i = 0;
while(i < Size)
{
printf("%d \t ", a[i]);
i++;
}
printf("\n");
}
The output of the above c program; is as follows:
lease Enter the Size of an Array : 5 Please Enter the Array Elements : 1 2 3 -5 -4 Total Number of Positive Numbers in this Array = 3 Array Elements in Positive Array : 1 2 3 Total Number of Negative Numbers in this Array = 2 Array Elements in Negative Array : -5 -4
C Program To Put Positive and Negative Numbers in two Separate Arrays using Function
/* C Program to Put Positive and Negative Numbers in two Separate Arrays */
#include<stdio.h>
void CountPositiveNumbers(int a[], int Size);
void CountNegativeNumbers(int a[], int Size);
void PrintArray(int a[], int Size);
int main()
{
int Size, i, a[10];
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]);
}
CountPositiveNumbers(a, Size);
CountNegativeNumbers(a, Size);
return 0;
}
void CountPositiveNumbers(int a[], int Size)
{
int i, Positive[10], Positive_Count = 0;
printf("\n List of Array Elements in Positive Array: ");
for(i = 0; i < Size; i ++)
{
if(a[i] >= 0)
{
Positive[Positive_Count] = a[i];
Positive_Count++;
}
}
PrintArray(Positive, Positive_Count);
printf(" Total Number of Positive Numbers in this Array = %d ", Positive_Count);
}
void CountNegativeNumbers(int a[], int Size)
{
int i, Negative[10], Negative_Count = 0;
printf("\n List of Array Elements in Negative Array: ");
for(i = 0; i < Size; i ++)
{
if(a[i] < 0)
{
Negative[Negative_Count] = a[i];
Negative_Count++;
}
}
PrintArray(Negative, Negative_Count);
printf(" Total Number of Negative Numbers in this Array = %d ", Negative_Count);
}
void PrintArray(int a[], int Size)
{
int i;
for(i = 0; i < Size; i++)
{
printf("%d \t ", a[i]);
}
printf("\n");
}
The output of the above c program; is as follows:
Please Enter the Size of an Array : 5 Please Enter the Array Elements : 1 -2 -3 5 6 List of Array Elements in Positive Array: 1 5 6 Total Number of Positive Numbers in this Array = 3 List of Array Elements in Negative Array: -2 -3 Total Number of Negative Numbers in this Array = 2