How to display the parent category if only the child category has been chosen

There’s a function called get_category_parents that will return a list of parents based on the ID. You can then loop through this list to display the parents.