The code is below -