Root cause is almost always inside the water heater. Gas and electric fail differently. Gas: pilot light, thermocouple, gas control valve. Electric: one or two heating elements controlled by thermostats. Many fixes you can troubleshoot yourself, but gas lines and 240-volt wiring should be left to a pro.