There are few simple rules to follow.
(1) The is equal to the number of protons.
(2) In neutrally charged , the number of electrons is the same as the number of protons.
Otherwise, positive charge means that the element lost an electron and negative charge means it gained an electron.
(3) The is equal to the sum of the number of protons and number of neutrons.
or
So if you say that
40 = 20 + number of neutrons
40 - 20 = number of neutrons
Therefore,
number of neutrons = 20