-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumAvgArray.c
More file actions
58 lines (46 loc) · 1.26 KB
/
Copy pathSumAvgArray.c
File metadata and controls
58 lines (46 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
C program to read N integers into an array A and
a) Find the sum of negative numbers
b) Find the sum of positive numbers
c) Find the average of all numbers
*/
#include <stdio.h>
void main()
{
int array[10];
int i, num, negative_sum = 0, positive_sum = 0;
float total = 0.0, avg;
printf("Enter the value of N \n");
scanf("%d", &num);
printf("Enter %d numbers (-ve, +ve and zero) \n", num);
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements \n");
for (i = 0; i < num; i++)
{
printf("%+3d\n", array[i]);
}
//Summation starts
for (i = 0; i < num; i++)
{
if (array[i] < 0)
{
negative_sum = negative_sum + array[i];
}
else if (array[i] > 0)
{
positive_sum = positive_sum + array[i];
}
else if (array[i] == 0)
{
;
}
total = total + array[i];
}
avg = total / num;
printf("\nSum of all negative numbers = %d\n", negative_sum);
printf("Sum of all positive numbers = %d\n", positive_sum);
printf("\nAverage of all input numbers = %.2f\n", avg);
}