CLAREMONT, Calif. — When Maria Klawe became president of Harvey Mudd College in 2006, she was dismayed — but not surprised — at how few women were majoring in computer science.

A mathematician and computer scientist herself, she arrived at Harvey Mudd (the smallest of the five so-called Claremont Colleges) in the midst of a nationwide downturn for women in computer science. As recently as 1985, 37 percent of graduates in the field were women; by 2005 it was down to 22 percent, and sinking.

And the situation at Mudd was even grimmer. Of the college’s 750 students, about a third were women (the figure is now closer to half), but for years the percentage of computer science graduates had been hovering around the single digits.

How Dr. Klawe (pronounced KLAH-vay) and her faculty turned things around — this year, nearly 40 percent of Harvey Mudd’s computer science degrees will go to women — sheds light on a gender gap that elsewhere remains stubbornly resistant to changing times.

Thanks in part to companies like Facebook, Yelp and Zynga and in part to cultural sensations like the movie “The Social Network,” coders are hip and computer science is hot. Departments across the nation are brimming with students.