Problem Description
Raju was playing game with somu. Raju will tell set of positive numbers and negative numbers. Somu need to tell the sum of positive numbers and sum of negative numbers. Kindly help Raju and Somu to complete the task.CODING ARENA
#include <stdio.h>
int main()
{
int a[20],i,n,ps=0,ns=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]>0)
ps=ps+a[i];
if(a[i]<0)
ns=ns+a[i];
}
printf("Sum of positive elements is:%d",ps);
printf("\nSum of negative elements is:%d",ns);
return 0;
}
Test Case 1
Input (stdin)7 -3 -2 1 4 6 -4 9
Expected OutputSum of positive elements is:20 Sum of negative elements is:-9
Test Case 2
Input (stdin)5 -1 -2 -3 5 4
Expected OutputSum of positive elements is:9 Sum of negative elements is:-6
No comments:
Post a Comment