C program to read N integers (zero, +ve and -ve) into an array A and to a) Find the sum of negative numbers b) Find the sum of positive numbers and c) Find the average of all input numbers Output the various results computed with proper headings

by Nideesh C on April 12, 2011 · 0 comments

in C Programming




/* Write a C program to read N integers (zero, +ve and -ve) into an array A and to a) Find the sum of negative numbers
b) Find the sum of positive numbers and
c) Find the average of all input numbers
Output the various results computed with proper headings */

#include <stdio.h>
#include <conio.h>
#define MAXSIZE 10

void main()
{
int array[MAXSIZE];
int i, N, negsum=0, posum=0;
float total=0.0, averg;

clrscr();

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

printf(“Enter %d numbers (-ve, +ve and zero)\n”, N);
for(i=0; i< N ; i++)
{
scanf(“%d”,&array[i]);
fflush(stdin);
}

printf(“Input array elements\n”);
for(i=0; i< N ; i++)
{
printf(“%+3d\n”,array[i]);
}

/* Summing  begins */
for(i=0; i< N ; i++)
{

if(array[i] < 0)
{
negsum = negsum +  array[i];
}
else if(array[i] > 0)
{
posum = posum + array[i];
}
else if( array[i] == 0)
{
;
}
total = total + array[i] ;
}

averg = total / N;
printf(“\nSum of all negative numbers    = %d\n”,negsum);
printf(“Sum of all positive numbers    = %d\n”, posum);
printf(“\nAverage of all input numbers   = %.2f\n”, averg);

}        /*End of main()*/
/*————————————-
Output
Enter the value of N
5
Enter 5 numbers (-ve, +ve and zero)
5
-3
0
-7
6
Input array elements
+5
-3
+0
-7
+6

Sum of all negative numbers    = -10
Sum of all positive numbers    = 11

Average of all input numbers   = 0.20
————————————–*/

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C program to find the sum of odd numbers and sum of even numbers from 1 to N. Output the computed sums on two different lines with suitable headings
  2. C program to find the number of integers divisible by 5 between the given range N1 and N2, where N1 < N2 and are integers. Also find the sum of all these integer numbers that divisible by 5 and output the computed results
  3. Write a C program to find the sum of ‘N’ natural numbers
  4. C program to find the GCD and LCM of two integers output the results along with the given integers. Use Euclids’ algorithm
  5. Sample – Program to find the sum of each row & column of a matrix of size n x m and if matrix is square, find the sum of the diagonals also..

Leave a Comment

Previous post:

Next post: