Where to add custom code?
By the way, you can add this code snippet at your child theme functions.php file or you can use any third-party plugin like this( https://wordpress.org/plugins/code-snippets/ ).
After adding those code as snippet on your site you will see that, 5 related product will display at the buttom of the single product page. Check below screenshots
So you can easily change your related products number as you want.
That’s All.