Sunday 15 March 2015

To find the sum of +VE & -VE elements in an array Using C

# include <stdio.h>
# include <conio.h>

void main()
{
int a[20], i, n, psum = 0, nsum = 0 ;
clrscr() ;
printf("Enter the limit : ") ;
scanf("%d", &n) ;
printf("\nEnter the elements :\n\n") ;
for(i = 0 ; i < n ; i++)
scanf("%d", &a[i]) ;
for(i = 0 ; i < n ; i++)
{
if(a[i] > 0)
psum = psum + a[i] ;
if(a[i] < 0)
nsum = nsum + a[i] ;
}
printf("\nSum of positive elements is : %d", psum) ;
printf("\n\nSum of negative elements is : %d", nsum) ;
getch() ;
}

RUN 1 :
~~~~~~~
Enter the limit : 5
Enter the elements :
-10 30 50 -20 40
Sum of positive elements is : 120
Sum of negative elements is : -30

No comments:

Post a Comment