Sum of negative and positive integers of the array
C program to find sum of negative and positive integers of the array.
Program
#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
int arr[MAX];
int i, n, sum_neg, sum_pos;
printf("Enter the number of elements in the array\t:");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
{
printf("Element %d\t:",i+1);
scanf("%d",&arr[i]);
}
sum_neg = 0;
sum_pos = 0;
for(i=0;i<n;i++)
{
if(arr[i]>0)
sum_pos = sum_pos + arr[i];
else
sum_neg = sum_neg + arr[i];
}
printf("\nSum of the positive elements of the array is\t:%d",sum_pos);
printf("\nSum of the negative elements of the array is\t:%d",sum_neg);
getch();
}
Output
Enter the number of elements in the array :8
Enter the elements
Element 1 :1
Element 2 :-3
Element 3 :4
Element 4 :-5
Element 5 :9
Element 6 :-2
Element 7 :9
Element 8 :-4
Sum of the positive elements of the array is :23
Sum of the negative elements of the array is :-14