What steps have you taken to inspect the return value of your distance() function?
Also consider re-reading sprintf and printf and the replies you already got. Many of them addressed two problems in your program, but changing the formula is only one problem.