Skip to main content

Featured

C Program to Solve Two Sum Using Brute Force (With Algorithm & Output)

 Introduction The Two Sum problem is a popular coding interview question where we must find two indices of an array whose values add up to a given target. This program demonstrates a simple brute-force solution in C using nested loops and dynamic memory allocation. Problem Statement Given an integer array and a target value, return the indices of the two numbers such that they add up to the target. Each input has exactly one solution, and the same element cannot be used twice. The result should return the indices, not the values. If no solution exists, return NULL.  Algorithm / Logic Explanation Start the program. Traverse the array using a loop from index 0 to numsSize - 1 . Inside this loop, use another loop starting from i + 1 to numsSize - 1 . For every pair (i, j) , check if nums[i] + nums[j] == target . If condition becomes true: Allocate memory for 2 integers using malloc() . Store indices i and j . Set returnSize = 2 . Return the result poi...

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

๐ŸŒ™