Reusing logic to set string variables
I would use macros. A Twig macro is basically a function, that can take arguments and render HTML.

Macros can be defined inline (i.e. in the same template where it's used) or in a separate file (better if you're writing a generic macro that'll possibly be used in several different templates).

Here's how your template could look, using macros:

{% macro courseName(entry) %}
    {{ entry.deptCode ~ ' ' ~ entry.courseNumber ~ ': ' ~ entry.title }}
{% endmacro %}

{% macro msuLink(entry) %}
    {{ 'https://reg.msu.edu/Courses/Request.aspx?SubjectCode=' ~
entry.deptCode ~ '&amp;CourseNumber=' ~ entry.courseNumber ~ '#Results' }}
{% endmacro %}

{% import _self as helpers %}

&lt;h2&gt;Required Courses&lt;/h2&gt;
&lt;ul&gt;
    {% for entry in craft.entries.section('courses').type('safsCourse') %}
        &lt;li&gt;&lt;a href="{{ entry.url }}"&gt;{{ helpers.courseName(entry) }}&lt;/a&gt;&lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;

&lt;h2&gt;Elective Courses&lt;/h2&gt;

&lt;h3&gt;At least one agricultural science course&lt;/h3&gt;
&lt;ul&gt;
    {% for entry in craft.entries.relatedTo(65) %}
        &lt;li&gt;&lt;a href="{{ helpers.msuLink(entry) }}"&gt;{{ helpers.courseName(entry) }}&lt;/a&gt;&lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;

...


If you're wondering about the {% import _self as helpers %} directive below the actual macros, the {% import %} tag is neccessary to actually expose your macros to your template (even if the macros are declared in the same file), and the "helpers" part is just a contextual variable that could be named anything you want (as semantic as possible is always a good thing).