Based on: Get a verification code and sign in with two-factor authentication - Apple Support
A code is sent first to your trusted devices automatically. If you have a trusted device running iOS 9 and later, OS X El Capitan and later, iPadOS 13 and later, or watchOS 6 and later, the verification code is displayed automatically on your trusted devices.
If you can’t sign in, reset your password, or receive verification codes, you can request account recovery to regain access to your account [ How to use account recovery when you can’t reset your Apple Account password - Apple Support ]. Account recovery might take a few days or longer, depending on the specific account information you can provide to verify your identity.
To see, add, or change your trusted phone numbers use one of these methods:
● On your iPhone, iPad, or iPod touch: Go to Settings > your name > Sign-In & Security. Tap Two-Factor Authentication, tap Edit (above the list of trusted phone numbers), then enter your device passcode when prompted. Add or remove the number, then tap Done.
● On a Mac computer choose Apple menu > System Settings (or System Preferences), then click your name (or Apple ID). Click Sign-In & Security, then add or remove a trusted phone number.
● Using a web browser:
1) Go to https://account.apple.com and sign in.
2) Follow Account Security options until you find one letting you change trusted numbers,
3) Click "+" to add a new number (you may be required to verify this number).
4) If you wish to remove an old number, click "-" next to the number.
Similar information for the iPhone can be found here --> Use two-factor authentication for your Apple Account on iPhone - Apple Support
If you had set up an Account Recovery Contact ( Set up an account recovery contact - Apple Support ) you can also get a code that way.