There are 13 animals in the barn. Some are chickens and some are pigs. There are 40 legs in all. How many of each animal are there?

Let's assume there are x chickens in the barn.

Therefore, there are (13 - x) pigs in the barn.

The number of legs from chickens: 2 * x = 2x
The number of legs from pigs: 4 * (13 - x) = 52 - 4x

The total number of legs is 2x + (52 - 4x) = 40.
This simplifies to 52 - 2x = 40.
By solving this equation, 2x = 12, so x = 6.

Therefore, there are 6 chickens and 13 - 6 = 7 pigs.