Write a C program to find the sum of positive negative elements in an array.
#include<stdio.h>
int main()
{
int n,i,sum_positive=0,sum_negative=0;
printf("Enter the number of elements in the array : ");
scanf("%d",&n);
int arr[n];
printf("Enter the elements of the array : ");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{
if(arr[i]>0)
{
sum_positive=arr[i]+sum_positive;
}
else
{
sum_negative=arr[i]+sum_negative;
}
}
printf("Sum of positive numbers in the array = %d",sum_positive);
printf("\nSum of negative numbers in the array = %d",sum_negative);
}
Comments
Leave a comment