Given a natural number N. Calculate the difference between the largest and smallest digit in the record
Given a natural number N. Calculate the difference between the largest and smallest digit in the record of this number. C ++ program
#include <iostream>
#include <string> // library for working with strings (number size is not specified)
using namespace std;
int main () {
string s;
int mi = 10; // minimum digit of the number
int ma = -1; // maximum digit of the number
cin >> s;
int a [s.size ()]; // array of numbers
for (int i = 0; i <s.size (); i ++) {
char c; // one character
c = s [i]; // write a digit
a [i] = c – 48; // convert character to number
}
for (int i = 0; i <s.size (); i ++) {// looking for the maximum and minimum of the received numbers
if (a [i] <mi) {
mi = a [i];
}
if (a [i]> ma) {
ma = a [i];
}
}
cout << (ma-mi); // display the answer to the task
}