To find the sum of VE & -VE elements in an array
Program:
#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();
}
Output:
Enter the limit : 5
Enter the elements :
-10 30 50 -20 40
Sum of positive elements is : 120
Sum of negative elements is : -30
-
UpdatedDec 30, 2019
-
Views4,944
You May Like
Program to maintain employee details using structures
Program to maintain student details using structures
Check whether the person is eligible to vote or not
Print the numbers that are divisible by a given no.
Program to generate magic square
To sort the given numbers in ascending & descending order