Sorry to hear that. Your iMac keeps rebooting may be due to insufficient storage space, heavy CPU consumption, interference from incompatible programs, etc.
Have you checked your disk space? If not, go to the Apple menu > About This Mac > Storage. If the reason is insufficient storage space, organize files on your Mac and free up more space.
If that's not the case, reboot your Mac in Safe Mode to verify if the troublemaker is the third-party program. Then, uninstall suspicious third-party apps like the browser.
If your Mac keeps crashing after trying the above two tricks, the startup disk may have some problems. I suggest you run First Aid to check and repair the Mac hard drive first. If still no luck, try to erase your Mac and reinstall macOS.
For the data on your Mac, if your Mac can start up and keep working, simply copy the files. If it restarts itself more frequently and gives you no chance to back up files, or it eventually won't turn on, you can use iBoysoft Mac Recovery Mode to recover data from your crashed iMac.