x is five years older than y
x= 5+y
(Adding 5 years to y will make both equal)
z is 6 years older than x
z= 6+x
In terms of y:
z= 6 + (5+y)
x+y+z = 31
(5+y) + y + 6 + (5+y) = 31
16 + 3y =31
3y = 15
y= 5
The three ages are
y= 5
x= (5+y) = 10
z= (6+5+y)= 16
16+10+5= 31