How to display the number of posts in a category using WP Query

You can use the found_posts property, which is the total number posts for the current query:

echo $the_query->found_posts;