Let the number of ducks in John’s pen be x.
The number of pigs is 3*x = 3x.
A duck has two legs, and a pig has four.
Therefore, total number of legs in his pen is:
x*2 + 3x*4 = 2x+12x = 14x
By the condition of the problem,
14x = 40
x = 40/14 = 20/7 = 6 6/7
This is a fraction. John can not have a fractional number of living beings; be it a duck or a pig in his pen.
So there must be something wrong with the statement of the problem.
(Assuming that john has 3 times the ducks than he has the pigs, then total number of legs in his pen would be:
3x*2+x*4 = 6x+4x = 10x
and, the problem would then require,
10x = 40
x = 4
Number of ducks = 12, number of pigs = 4)