C Online Compiler
Example: Min-Max Sum of Array in C Program
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// 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; }
6 45 65 3 78 3 90
Output
Clear
ADVERTISEMENTS