How can you log out of iCloud if you forget the password?
Since you bought it from a friend I would refer them to: What to do before selling or giving away your iPhone, iPad, or iPod touch and ask them to take these steps.

You should also familarise yourself with the above info.

In summary though, before selling any iDevice, you should:


Do a full backup of your device
Unpair your Apple Watch (if applicable)
Sign out of iCloud (if applicable)
Also delete the device from Find My iPhone (if applicable)
Sign out of iTunes and the App Store
Do a full Erase of contents and settings


It's fortunate for both you and your friend that you bought your iPhone from a friend.