Q:

C program to find the difference between the largest and smallest element in the array

0

C program to find the difference between the largest and smallest element in the array

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Here, we will create an array of integers and find the difference between the largest and smallest element of the array and print the result on the console screen.

Program:

The source code to find the difference between the largest and smallest element in the array is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to find the difference between the
// largest and smallest element in the array

#include <stdio.h>

int main()
{
    int arr[] = { 10, 20, 70, 40, 50 };

    int i = 0;
    int j = 0;

    int diff = 0;

    diff = arr[1] - arr[0];

    for (i = 0; i < 5; i++) {
        for (j = i + 1; j < 5; j++) {
            if (arr[j] - arr[i] > diff)
                diff = arr[j] - arr[i];
        }
    }

    printf("Difference is: %d\n", diff);

    return 0;
}

Output:

Difference is: 60

Explanation:

Here, we created an array arr with 5 elements. And, we also created three variables ijdiff that are initialized with 0. Then we find the difference between the biggest and smallest elements of the array and store the result into a diff variable. After that, we printed the result on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

One Dimensional Array Programs / Examples in C programming language

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to print the square of array elements... >>
<< C program to segregate 1\'s and 0\'s in ...