and then suppose that it prints some output to the screen but takes a few hours to finish running. If I want to keep working on the same terminal while it runs, what is the command that I should use? I tried & at the end and it did not seem to work.

Since the program runs a long time, you may want to consider using screen. You can run the program and then start another command prompt with Control-a Control-c. You can do that as many times as you want, and be working on several things at once. Also, even if you log out and then back in later (just don't shut down), you can reconnect to screen and see how it's doing. Do "man screen" for info.

There is also tmux, which is a similar tool, but you'll need to install it.

I think screen will best answer your specific question, but it sounds like your workflow would benefit the most from using tabs.

To open a new tab in terminal, with the terminal selected you press Ctrl+T, and you can switch between tabs by pressing Ctrl+PgUp or Ctrl+PgDown to switch between them.

This quickly becomes intuitive, and ends up being efficient as you can quickly switch between tabs to check on long running programs, or quickly spawn more tabs as you busy the ones you're working in with long tasks.