University Course Selection

This program prints course name based on course code using switch.

Problem Statement

Write a Java program that prints course name for a given course code.

Source Code

1 public class CourseSelection {
2 public static void main(String[] args) {
3 int courseCode = 2;
4 switch(courseCode) {
5 case 1: System.out.println("Computer Science - Eduinq"); break;
6 case 2: System.out.println("Information Technology - Eduinq"); break;
7 case 3: System.out.println("Mechanical Engineering - Eduinq"); break;
8 case 4: System.out.println("Civil Engineering - Eduinq"); break;
9 default: System.out.println("Invalid course code - Eduinq");
10 }
11 }
12 }

Program Output

Information Technology - Eduinq

Explanation

The program uses a switch statement to map course codes to course names. Each case corresponds to a course. If the code matches, the program prints the selected course. If no case matches, the default block prints invalid course code. This demonstrates how switch can be used to implement code-to-name mapping in educational contexts. Shows how switch can map course codes to course names.

Quick Links to Explore