Check the model of the MacBook which you can see at the bottom of the iMac stand if I remember correctly.
Sounds like a 2011 27” iMac with the faulty GPU which is common on that model. Not worth fixing and better to replace with something else instead.