The contacts you have added through your accounts, such as those of Google or Whatsapp, are automatically saved in the address books of the respective apps. If you intend to format your device, you need to back up the contacts you have saved directly in the phone memory, so as not to lose them. The easiest way to do this is to copy them to your Google account.
Steps
Part 1 of 3: Finding Your Contacts

Step 1. Press the Contacts or People app on your device
This will vary depending on the manufacturer of your device and the Contacts app you are using.

Step 2. Press the ⋮ or More button
You will usually find it in the upper right corner of the screen.

Step 3. Press Contacts to View or View Options
You may need to press the Settings button first. The exact procedures vary from device to device.

Step 4. Press an account to view its contacts
Once you have selected an account, you will see all the contacts saved in it. All entries linked to the account will be copied automatically and can be restored upon login.
For example, pressing "WhatsApp" will display all the contacts of that app. These entries are saved on WhatsApp's servers, so you don't have to worry about saving a copy

Step 5. Press Phone to view the contacts stored on your phone
These are the items saved on the device memory, which you have to transfer to another account, Google for example, or export as a file. The contacts saved on the phone memory are deleted if you perform a factory reset.
Part 2 of 3: Copy Contacts from Phone to Google

Step 1. Open the Contacts app and view the Phone section
You should only see the entries saved on the device memory.
Note that the terms used in these sections vary a lot, depending on the manufacturer of your phone. The features discussed here may not be available on all devices

Step 2. Press the More or ⋮ button

Step 3. Press Settings or Manage contacts

Step 4. Press Move device contacts to or Copy
The terms used for this option vary greatly depending on the device. Look for a tool that allows you to transfer contacts from one account to another.
If you don't have the ability to copy contacts to your Google account, you can still export them to a file and import them later

Step 5. Press Phone in the From list
If you are asked to select the account from which to copy contacts, select the phone memory.

Step 6. Press your Google account in the A
Select it from the list of accounts to which you can move contacts; this way you can be sure that they will reappear when you log back into your Google account and you can view them on the contacts.google.com page.

Step 7. Press Copy or OK
The contacts will be copied to your Google account. It may take a few moments if you are transferring a lot of numbers.

Step 8. Visit contacts.google.com on an internet browser
Check if your contacts have been imported correctly.

Step 9. Log into your Google account
Make sure it's the same account you copied your contacts to.

Step 10. Find recently added contacts
If you see the numbers copied from your phone in the address book, you have saved them on Google. It may take a few minutes for the contacts to be synchronized.
Part 3 of 3: Export Your Contacts as a File

Step 1. Press the Contacts or People app on your device
If you don't have the ability to directly copy contacts to your Google account, you can export them to a file, then import that file to your Google profile.

Step 2. Press the ⋮ or More button

Step 3. Press Contacts to View or View Options
If you don't see these items, press the Settings button first.

Step 4. Press the Phone item
The Contacts app will only display the numbers saved on your device, i.e. the numbers you need to save a copy of.

Step 5. Press the ⋮ or More button

Step 6. Press Settings or Manage contacts

Step 7. Press Import / Export or Backup

Step 8. Press Export

Step 9. Select your device memory
In this way the contact file will be saved directly on the phone.

Step 10. Select the contacts to export
If you can, choose the contacts to export. Since you have chosen to only display numbers saved on your phone, you will usually be able to use the "Select all" feature.

Step 11. Wait for the operation to complete
You will see a notification appear at the top of the screen when the contacts have been exported.

Step 12. Press the ⋮ or More button in the Contacts app

Step 13. Press Settings or Manage contacts

Step 14. Press the Import / Export button

Step 15. Press Import

Step 16. Tap on your Google account
This way the contacts you exported will be added directly to your Google profile.

Step 17. Press the contact file
Press the file you just created when asked for the source of the contacts. This will import the phone numbers inside the file into your Google account, creating an online backup copy.

Step 18. Visit contacts.google.com on an internet browser

Step 19. Log into your Google account
Make sure it's the same account you copied your contacts to.

Step 20. Find recently added contacts
Look for the numbers you just imported from the phone. If you find them, they have been saved to Google and are now safe.