1) It's a combination of 2 chosen from 40:
⁴⁰C₂ = (40!)/[(2!).(40-2)!] = 780
2) Another way to think about this problem is as follows:
1st shakes hand with 39 (he can't shake his own hand)
2nd shakes hand with 38 (his hand was already shaken by the 1st, so no duplication)
3rd shakes hand with 37 (his hand was already shaken by the 1st, and 2nd, so no duplication)
And so on and so forth...until 1:
Number of hands shaken : 39 + 38 + 37 + 36 + ...+1
The sum = [(1+39).39]/2 = 40x39/2 = 780
Remember the above is an Arithmetic progression, with first term 1. the last term 39 and the number of terms 39
S=(a₁+a₃₉)(39/2)