Buy online before you arrive.
Entrance into either national park will be $35. If you're planning to spend more than two days in the parks, it's worth it to get the annual pass ($70). This can be purchased ahead of time online. Note that you will be mailed a physical annual pass to be sure to allow time for it to arrive before you leave for your trip. Remember YNP and GTNP are separate parks and will require separate entrance fees.
Go check out my guide for the best free things to do as well as itineraries and travel tips to make your trip unforgettable.