Skip to main content

Featured

C++ Program to Perform Linear Search on a Vector

  C++ Program to Perform Linear Search on a Vector Introduction In this C++ program, we will learn how to perform a Linear Search on a vector. The program first takes the size of the vector and its elements as input. Then it asks the user for the element to search. If the element is found, it displays the index where it is located. Otherwise, it displays a message indicating that the element is not found. C++ Program #include<bits/stdc++.h> using namespace std; int main() { int num, search, found = 0; cout << "Enter the size of the vector:" << endl; cin >> num; vector<int> v(num); cout << "Enter " << num << " elements in vector:" << endl; for(int i = 0; i < num; i++) { cin >> v[i]; } cout << "Enter element that you want to search:" << endl; cin >> search; for(int i = 0; i < num; i++) { ...

C Program to Print Day of the Week Using Enum and Switch

Day of the Week Using Enum and Switch in C

✅ C Program to Print Day of the Week Using Enum and Switch

#include <stdio.h>

// Define enum for days
typedef enum {
    SUNDAY,
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY
} Day;

int main() {
    Day today;

    int input;
    printf("Enter a number (0 for Sunday to 6 for Saturday): ");
    scanf("%d", &today);

    if (today < 0 || today > 6) {
        printf("Invalid day number!\n");
        return 1;
    }

    // Using switch to print the day name
    switch (today) {
        case SUNDAY:    printf("It's Sunday\n"); break;
        case MONDAY:    printf("It's Monday\n"); break;
        case TUESDAY:   printf("It's Tuesday\n"); break;
        case WEDNESDAY: printf("It's Wednesday\n"); break;
        case THURSDAY:  printf("It's Thursday\n"); break;
        case FRIDAY:    printf("It's Friday\n"); break;
        case SATURDAY:  printf("It's Saturday\n"); break;
    }

    return 0;
}
  

๐Ÿ“˜ Explanation:

This C program maps numbers (0 to 6) to corresponding days of the week using enum and prints the result using a switch statement.

  • typedef enum creates a readable set of named constants for days.
  • User is prompted to enter a number between 0 and 6.
  • If the input is invalid, the program exits with an error message.
  • The switch statement matches the input to its corresponding weekday.

๐Ÿงพ Sample Output:

Enter a number (0 for Sunday to 6 for Saturday): 2
It's Tuesday

Enter a number (0 for Sunday to 6 for Saturday): 7
In

Comments

Popular Posts

๐ŸŒ™