When a metal reacts with the oxygen and water(moisture) present in the atmosphere, then a coating of metal oxide is formed on the surface of the metal, which corrodes the metal. Painting of metal cuts of the moisture and oxygen from the metal surface and stops the reaction from happening, therefore, painting a metal prevents it from corroding.