Create a function to get the difference for the largest and smallest number in a positive number. For example integer 98721, the difference is 9 - 1, so the function would return 8. I have written this to solve it, but wonder if there is a better way of solving this problem. No array anyway, this question doesn't allow me to use array, only loop, if else and function.
int differenceMaxMin (int);
int main()
{
int n;
cout << "Enter a positive integer: "
cin >> n;
cout << "The differences of the largest and smallest number is: "
<< int differenceMaxMin (n) << endl;
}
int differenceMaxMin (int n)
{
int lastD, nextD, max, min;
lastD = n % 10;
max = lastD;
n = n / 10;
nextD = n % 10;
min = nextD;
while (n > 0)
{
if (max < nextD)
max = nextD;
elseif (min > nextD)
min = nextD;
n = n / 10;
nextD = n % 10;
}
return (max - min)
}