Method 1
#include <stdio.h>
int main()
{
int Arr[100], n, i, sum = 0;
printf("Enter the number of elements you want to insert : ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("Enter element %d : ", i + 1);
scanf("%d", &Arr[i]);
sum += Arr[i];
}
printf("\nThe sum of the array is : %d", sum);
printf("\nThe average of the array is : %0.2f", (float)sum / n);
return 0;
}
Output
Enter the number of elements you want to insert : 6
Enter element 1 : 10
Enter element 2 : 20
Enter element 3 : 15
Enter element 4 : 18
Enter element 5 : 21
Enter element 6 : 19
The sum of the array is : 103
The average of the array is : 17.17
Method 2
#include <stdio.h>
#define MAXSIZE 10
void main()
{
int array[MAXSIZE];
int i, num, negative_sum = 0, positive_sum = 0;
float total = 0.0, average;
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]);
}
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] ;
}
average = total / num;
printf("\n Sum of all negative numbers = %d\n", negative_sum);
printf("Sum of all positive numbers = %d\n", positive_sum);
printf("\n Average of all input numbers = %.2f\n", average);
}
Output
Enter the value of N
10
Enter 10 numbers (-ve, +ve and zero)
-8
9
-100
-80
90
45
-23
-1
0
16
Input array elements
-8
+9
-100
-80
+90
+45
-23
-1
+0
+16
Sum of all negative numbers = -212
Sum of all positive numbers = 160
Average of all input numbers = -5.20