Practical / Solution
Enumerated Data Types — Exercise 1
Problem
Write a C program to create an enumeration for the days of the week
and display a selected day.
Program
#include <stdio.h>
enum Day
{
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
};
int main()
{
enum Day today;
today = WEDNESDAY;
printf("Today is day number %d\n", today);
return 0;
}
Explanation
An enumeration (enum) defines a set of named integer
constants. By default, the first enumerator starts with value 0 and
subsequent values increase by 1.
Expected Output
Enumerated Data Types — Exercise 2
Problem
Write a C program to create an enumeration for months and display
the numeric value of a selected month.
Program
#include <stdio.h>
enum Month
{
JANUARY = 1,
FEBRUARY,
MARCH,
APRIL,
MAY,
JUNE,
JULY,
AUGUST,
SEPTEMBER,
OCTOBER,
NOVEMBER,
DECEMBER
};
int main()
{
enum Month month;
month = AUGUST;
printf("Month number = %d\n", month);
return 0;
}
Explanation
Enumeration values can also be assigned explicitly. Here,
JANUARY is assigned 1, so the following months receive
consecutive values.
Expected Output
Enumerated Data Types — Exercise 3
Problem
Write a C program to create an enumeration for traffic signal colors
and use a switch statement to display the signal meaning.
Program
#include <stdio.h>
enum Signal
{
RED,
YELLOW,
GREEN
};
int main()
{
enum Signal signal;
signal = GREEN;
switch (signal)
{
case RED:
printf("Stop\n");
break;
case YELLOW:
printf("Wait\n");
break;
case GREEN:
printf("Go\n");
break;
}
return 0;
}
Explanation
An enum can make program logic easier to read by replacing numeric
constants with meaningful names. These names can also be used with
switch statements.
Expected Output
Enumerated Data Types — Exercise 4
Problem
Write a C program to create an enumeration for student grades and
display the grade selected in the program.
Program
#include <stdio.h>
enum Grade
{
FAIL,
PASS,
GOOD,
VERY_GOOD,
EXCELLENT
};
int main()
{
enum Grade grade;
grade = EXCELLENT;
printf("Grade code = %d\n", grade);
return 0;
}
Explanation
Enumeration constants provide meaningful names for integer values.
In this example, EXCELLENT represents one of the defined
grade categories.
Expected Output
Enumerated Data Types — Exercise 5
Problem
Write a C program to take a numeric choice from the user and use an
enumeration with a switch statement to display the selected operation.
Program
#include <stdio.h>
enum Operation
{
ADD = 1,
SUBTRACT,
MULTIPLY,
DIVIDE
};
int main()
{
enum Operation op;
int choice;
printf("1. Add\n");
printf("2. Subtract\n");
printf("3. Multiply\n");
printf("4. Divide\n");
printf("Enter choice: ");
scanf("%d", &choice);
op = (enum Operation)choice;
switch (op)
{
case ADD:
printf("Addition selected.\n");
break;
case SUBTRACT:
printf("Subtraction selected.\n");
break;
case MULTIPLY:
printf("Multiplication selected.\n");
break;
case DIVIDE:
printf("Division selected.\n");
break;
default:
printf("Invalid choice.\n");
}
return 0;
}
Explanation
Enumeration constants can be used to make menu-driven programs easier
to understand. The entered integer is converted to the enumeration type
and then processed using switch.
Expected Output
1. Add
2. Subtract
3. Multiply
4. Divide
Enter choice: 3
Multiplication selected.
Enumerated Data Types — Exercise 6
Problem
Write a C program to create an enumeration for account status and
display an appropriate message using a switch statement.
Program
#include <stdio.h>
enum AccountStatus
{
ACTIVE,
INACTIVE,
BLOCKED,
CLOSED
};
int main()
{
enum AccountStatus status;
status = BLOCKED;
switch (status)
{
case ACTIVE:
printf("Account is active.\n");
break;
case INACTIVE:
printf("Account is inactive.\n");
break;
case BLOCKED:
printf("Account is blocked.\n");
break;
case CLOSED:
printf("Account is closed.\n");
break;
}
return 0;
}
Explanation
Enums are especially useful when a variable can have one of a fixed
set of states, such as active, inactive, blocked or closed.
Expected Output
Enumerated Data Types — Exercise 7
Problem
Write a C program to create an enumeration for the four seasons and
display the name of the selected season.
Program
#include <stdio.h>
enum Season
{
SPRING,
SUMMER,
AUTUMN,
WINTER
};
int main()
{
enum Season season;
season = WINTER;
switch (season)
{
case SPRING:
printf("Spring season\n");
break;
case SUMMER:
printf("Summer season\n");
break;
case AUTUMN:
printf("Autumn season\n");
break;
case WINTER:
printf("Winter season\n");
break;
}
return 0;
}
Explanation
An enumeration is useful when a variable should represent one value
from a predefined group. The named constants make the program easier
to understand than using unexplained numbers.
Expected Output
Enumerated Data Types — Exercise 8
Problem
Write a C program to create an enumeration for employee departments
and display the selected department.
Program
#include <stdio.h>
enum Department
{
HR,
SALES,
IT,
FINANCE
};
int main()
{
enum Department department;
department = IT;
switch (department)
{
case HR:
printf("Human Resources\n");
break;
case SALES:
printf("Sales Department\n");
break;
case IT:
printf("Information Technology\n");
break;
case FINANCE:
printf("Finance Department\n");
break;
}
return 0;
}
Explanation
Enumeration constants can represent fixed categories such as company
departments. The descriptive names make the code easier to read and maintain.
Expected Output
Enumerated Data Types — Exercise 9
Problem
Write a C program to create an enumeration for payment methods and
display the selected payment method.
Program
#include <stdio.h>
enum PaymentMethod
{
CASH = 1,
CARD,
UPI,
NET_BANKING
};
int main()
{
enum PaymentMethod payment;
payment = UPI;
switch (payment)
{
case CASH:
printf("Payment Method: Cash\n");
break;
case CARD:
printf("Payment Method: Card\n");
break;
case UPI:
printf("Payment Method: UPI\n");
break;
case NET_BANKING:
printf("Payment Method: Net Banking\n");
break;
}
return 0;
}
Explanation
Enums can represent a fixed set of options in real-world applications.
Here, each payment method has a meaningful named constant.
Expected Output
Enumerated Data Types — Exercise 10
Problem
Write a C program to create a student attendance status using an
enumeration and display whether the student is present, absent or on leave.
Program
#include <stdio.h>
enum Attendance
{
PRESENT = 1,
ABSENT,
ON_LEAVE
};
void displayAttendance(enum Attendance status)
{
switch (status)
{
case PRESENT:
printf("Student is Present.\n");
break;
case ABSENT:
printf("Student is Absent.\n");
break;
case ON_LEAVE:
printf("Student is on Leave.\n");
break;
default:
printf("Invalid attendance status.\n");
}
}
int main()
{
enum Attendance status;
status = PRESENT;
displayAttendance(status);
return 0;
}
Explanation
This example combines an enumeration with a function and a
switch statement. The enum clearly represents the limited
set of attendance states.
Expected Output