"Why is programming fun? What delights may its practitioner expect as his reward? First is the sheer joy of making things. As the child delights in his mud pie, so the adult enjoys building things, especially things of his own design. I think this delight must be an image of God's delight in making things, a delight shown in the distinctness and newness of each leaf and each snowflake."
-- Frederick P. Brooks, Jr. from MythicalManMonth
While some people write programs to solve important problems, or just to get a paycheck, many programmers actually enjoy the activity. Many mere mortals can't understand why.
Here are some of the things that programmers enjoy about programming:
Creativity
Programming is, in a way, like writing poetry or music. It is an intellectual activity where one creates structure out of nothing. And the things one creates can have significant value to others.
See also SoftwareIsArt
Model Building
One of the things a programmer does is to create elaborate logical models of the world (or other domains). A programmer constructs a bunch of things in an imaginary universe, and then sets them all in motion. And the programmer gets to set and to change the laws of these universes. This is fun in the same way that playing with model trains is fun.
Learning How Stuff Works
Some people enjoy taking things apart and figuring out how they work. Programming takes this to another level--not only do you figure out how things work, but you can also figure out how to write code to make them do things differently.
Programmers get to learn how networks work, how telephones and modems work, and learn a lot about the domains for which they are writing applications.
Knowing more than other people do is a rush.
Learning New Things
Programmers are constantly using new operating systems, new programming languages, new database management systems, new libraries, etc.
Power
One of the things programmers do that makes them and their associates say "Wow, that's cool" is when a small amount of effort results in a huge change in a system's behavior. (Of course, the danger is that these widespread changes will have undesired effects.)
Programmers, even the most junior ones, make decisions every minute that impact the users of their programs.
Puzzle-Solving
Programmers solve lots of complicated problems. The more complicated, the more enjoyable they are to solve.
Some people solve those problems such as from InternationalCollegiateProgrammingContest "just for fun".
Adulation of One's Peers
Programmers love it when other programmers look at their work and say "Cool! How did you do that?"
Seeing One's Creations Live On
There's something special about walking into a shop or bank or whatever and seeing one's work living in the world with its own independent existence. Knowing that code one has written is executed thousands of times every day is quite a thrill.
Getting From It What You Put Into It
The counter-point to ProgrammingIsNotFun's "Most Programs are Boring" item: If you are bored, you can always inject new life into a problem by trying to automate it, abstract it, or otherwise solve a larger problem simultaneously. See: BoredomIsaSmell
Deep Concentration
Programming often involves going into a deep form of concentration, somewhat like meditation (see MentalStateCalledFlow). This feeling is enjoyable.
Getting stuff done
It's a craft, a trade. In the evening, you hopefully see the results of your doing, very much unlike some consulting or management jobs.
I really connect with this idea. This probably seems silly, but I always think of the part at the end of Pretty Woman where Richard Gere gets into a Newport-News-like defense contracting business after having made all his money not really producing anything. His new business partner says "we're going to build greaat big ships together." I think that's a good analogy that describes the difference between what I do and what a lot of other people above and around me do.
I once heard MartinFowler say something like "programming is hard because its closest two traditional jobs would be mathematics and philosophy - except that unlike philosophers programmers need to produce something, and unlike mathematicians programmers have no proof of anything." (far from a quote, is a distantly remembered paraphrase). Anyway, I remember thinking that those are the things that make it fun. -- BrianMcCallister
See also FanaticOrientedProgramming, DisciplineAndDogma.
Contrast with ProgrammingIsNotFun