This computer problem can be usually attributed to a hardware issue. Usually its the power supply is dying or in most cases a dirty or defective cooling fan.
The computer trying to cool itself by automatically switching off. So, if you feel a bit of extra heat coming out the computer. Clean the fan and make sure it running properly. If your fan is functional, check your power supply. Also, you will be surprised how many people fail to make sure the cables are attached tightly. This be the cause of this particular computer problem.