Reset an iMac to factory without passwords
First you'll need to turn off your iMac. Then press the power button and immediately hold down the Control and R keys until you see the Apple logo or spinning globe icon.

Release the keys and shortly afterward you should see the macOS Utilities window appear.


In this are four options: Restore from Time Machine Backup, Reinstall macOS, Get Help Online and Disk Utility. Click on the last one (Disk Utility) and then select Continue.

Next, you'll see a list of all the HDs currently attached to your Mac. In the left column are the names of each drive, while in the main panel are the details of the selected drive.

Look under the Internal section for your main drive. This is often called Macintosh HD and appears at the top of the list. Click on it so that it's highlighted, then click the Erase button from the options in the main panel. Use the same format suggested by the Erase on for your HD. 

Once you are done, come back again to the window macOS Utilities and from there select Reinstall macOS. All should go good in 25-30 minutes as it downloads from the internet.