Positive and negative numbers in two separate arrays
C program to put positive and negative numbers in two separate arrays
Program
#include<stdio.h>
#include<conio.h>
#define MAX 50
void read_array(int a[MAX], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Element %d\t:",i+1);
scanf("%d",&a[i]);
}
}
void print_array(int a[MAX], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Element %d\t:%d\n",i+1,a[i]);
}
}
void main()
{
int arr[MAX], pos[MAX], neg[MAX];
int n, i, n_pos, n_neg;
printf("Enter the number of elements in the array\t:");
scanf("%d",&n);
printf("Enter the elements of the array\n");
read_array(arr, n);
n_pos = 0;
n_neg = 0;
for(i=0;i<n;i++)
{
if(arr[i]>=0)
{
pos[n_pos] = arr[i];
n_pos++;
}
else
{
neg[n_neg] = arr[i];
n_neg++;
}
}
printf("\nPositive integers array\n");
print_array(pos, n_pos);
printf("\nNegative integers array\n");
print_array(neg, n_neg);
getch();
}
Output
Enter the number of elements in the array :10
Enter the elements of the array
Element 1 :2
Element 2 :-4
Element 3 :0
Element 4 :56
Element 5 :-38
Element 6 :-32
Element 7 :-43
Element 8 :38
Element 9 :23
Element 10 :12
Positive integers array
Element 1 :2
Element 2 :0
Element 3 :56
Element 4 :38
Element 5 :23
Element 6 :12
Negative integers array
Element 1 :-4
Element 2 :-38
Element 3 :-32
Element 4 :-43