Really depends on the loan. That is correct when the loan is paid off as 1 lump sum at the end of the term. If it is an amortising loan the calculation differs because the capital balance reduces each month. As far as I know there is no closed-form solution to the total interest paid on an amortising loan so you would need to work out the interest paid per period.
Knowing why you are trying to work this out would be helpful.