Blogger templates

Pages

Saturday, 7 July 2012

Write a C Program to Find the Sum of All the Positive and Negative Numbers in Array

Positive and Negative Number Sum

Statement of C Program: This Program reads N integers (+ve , -ve and zero) into an Array A and Finds
  1. The sum of positive numbers
  2. The sum of negative numbers
  3. The average of all input numbers.
#include<stdio.h>
#include<conio.h>
#define MAX 8
void main()
{
int Array[MAX] ;
int i , N ;

int negsum , posum;    
/* negsum stands for Sum of Negative Numbers and posum stands for Sum of Positive Numbers */

float total , average;
clrscr();
negsum = 0 ;
posum = 0;
total = 0;

printf(" Enter the value of N\n");
scanf("%d" , &N);

printf(" Enter %d numbers( -ve , +ve and 0 )\n" , N);
for(i=0 ; i<N ; i++)
{
scanf(" %d" , &Array[i] );
}

printf(" Array elements\n");
for(i=0 ; i<N ; i++)
{
printf("%d\n" , Array[i] );
}

for(i=0 ; i<N ; i++)
{
if(Array[i] < 0)
{
negsum + = Array[i];
}
else if(Array[i] > 0)
{
posum + = Array[i] ;
}
else if(Array[i] == 0)
{
       ;
}
total + = Array[i];
}
average = total / N;

printf(" Sum of all positive numbers =%d\n" , posum);
printf(" Sum of all negative numbers =%d\n" , negsum);
printf(" Average of all input numbers %f\n" , average);

}                                                  / * End of main() */

 Output:
 Enter the value of N
  5
 Enter 5 numbers (-ve , +ve and 0)
  6
 -7
  0
 -3
  5
 Array elements
  6
 -7
  0
 -3
  5
 Sum of all positive numbers = +11
 Sum of all negative numbers = -10
 Average of all input numbers = 0.20

                                                                   That's All

0 comments:

Post a Comment