Skip to main content

Featured

Merge Sort in C++

  Merge Sort in C++ Introduction Merge Sort is a popular sorting algorithm that follows the Divide and Conquer approach. It divides an array into smaller subarrays, recursively sorts those subarrays, and finally merges the sorted subarrays to produce a completely sorted array. In this tutorial, we will learn how to implement Merge Sort in C++ . The program divides the array into two halves using the mid index, recursively sorts both halves, and then combines them using the merge() function. Merge Sort has a time complexity of O(n log n) in the best, average, and worst cases. Table of Contents Algorithm C++ Program Input Sample Output Output Explanation Dry Run Flow of Execution Time Complexity Space Complexity Applications Key Points Interview Questions Frequently Asked Questions Keywords Conclusion Algorithm Start the program. Read the size of the array. Read the array elements from the user. Call the mer...

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

๐ŸŒ™