Hint
Extract the three digits and sort them in decreasing order (three swaps are enough). The largest number is x·100 + y·10 + z. For the smallest number the digits go in increasing order, but the first digit must not be 0 – so check the zeros separately.
Solution (C++)
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n; // a three-digit number, e.g. 174
int x = n / 100, y = n / 10 % 10, z = n % 10; // the digits 1, 7, 4
// sort the digits in decreasing order: x >= y >= z (three swaps are enough)
if (x < y) { int t = x; x = y; y = t; }
if (y < z) { int t = y; y = z; z = t; }
if (x < y) { int t = x; x = y; y = t; }
int largest = x * 100 + y * 10 + z; // 741
// the smallest: the digits in increasing order, but the first digit must not be 0
int smallest;
if (z != 0)
smallest = z * 100 + y * 10 + x; // 147
else if (y != 0)
smallest = y * 100 + z * 10 + x; // e.g. 505 -> 505 (the digits 5,5,0 -> 505)
else
smallest = x * 100; // two zeros, e.g. 500 -> 500
cout << largest - smallest << endl;
return 0;
}