﻿ C++ : Compute the difference between the highest and lowest

# C++ Exercises: Compute the difference between the highest number and the lowest number

## C++ Basic: Exercise-74 with Solution

Write a C++ program that accepts various numbers and computes the difference between the highest number and the lowest number. All input numbers should be real numbers between 0 and 1,000,000. The output (real numbers) may include an error of 0.01 or less.

Visual Presentation:

Sample Solution:

C++ Code :

``````#include <iostream> // Header file for input/output stream operations
using namespace std;

int main()
{
double num[52]; // Array to store up to 52 double numbers
int c = 0; // Counter variable initialized to 0

// Loop to read numbers from input and store them in the 'num' array
while (cin >> num[c++]); // Reads numbers until invalid input is encountered and increments the counter 'c'

sort(num, num + c - 1); // Sorting the array of numbers

// Displaying the difference between the highest and lowest numbers entered
cout << "Difference between the highest number and the lowest number: ";
cout << num[c - 2] - num[0] << endl; // Output the difference between the highest and lowest numbers

return 0; // Indicating successful completion of the program
}
``````

Sample Output:

```Sample Input: 2 5
Difference between the highest number and the lowest number: 3
```

Flowchart:

C++ Code Editor:

What is the difficulty level of this exercise?

﻿