Min-Max Sum of Array in C Program
Min-max sum of array in C program.
In this article, you will learn how to make a min-max sum of array in C program.
Example
Enter the size of the array::
6
Enter the 6 elements of the array::
45 65 3 78 3 90
MIN SUM:: 194
MAX SUM:: 278
You should have knowledge of the following topics in c programming to understand this program:
- C
main()
function - C
printf()
function - C
while
loop - C Data Type
Source Code
// Min-Max Sum of Array in C Program
#include <stdio.h>
#include <math.h>
int main() {
int s=0;
printf("Enter the size of the array::\n");
scanf("%d", &s);
int arr[s];
printf("\nEnter the %d elements of the array::\n", s);
int i=0;
while (i<s) {
scanf("%d", &arr[i]);
i++;
}
double min=INFINITY, max=-INFINITY;
int l=s, min_sum=0, max_sum=0, counter=0;
// It will get the lowest and highest value from the array
while (l--) {
if (arr[l] < min) {
min=arr[l];
}
if (arr[l] > max) {
max=arr[l];
}
if (arr[l]==arr[s-1]) {
counter++;
}
}
// It will calculate the minimum & maximum sum from the array
if (counter == s) {
l=s-1;
while (l--) {
min_sum += arr[l];
}
max_sum=min_sum;
} else {
l=s;
while (l--) {
if (arr[l]!=max) {
min_sum += arr[l];
}
if (arr[l]!=min) {
max_sum += arr[l];
}
}
}
// It will print the final output of the program
printf("MIN SUM:: %d\n", min_sum);
printf("MAX SUM:: %d\n", max_sum);
return 0;
}
Output
Enter the size of the array::
6
Enter the 6 elements of the array::
45 65 3 78 3 90
MIN SUM:: 194
MAX SUM:: 278
Explanation
In this given program, we have taken the input size of the array 6
and elements of the array 45 65 3 78 3 90
from the user. Then applied the standard calculation to find the minimum & maximum sum of the values from the given array.
Then It will return the minimum sum that is 194
and the maximum sum is 278
.