Skip to main content

Featured

C Program to Check Prime Number Using Efficient Logic

  Introduction A prime number is a number that has exactly two distinct positive divisors: 1 and itself. In this program, we check whether a given number is prime or not using a simple and efficient logic. This type of program is commonly used in mathematics, competitive programming, and basic algorithm learning for beginners in C programming. Problem Statement The task is to write a C program that determines whether a given integer is a prime number or not. The program takes a single integer input from the user and analyzes its divisibility. If the number has no divisors other than 1 and itself, it should be identified as a prime number; otherwise, it is not prime. This problem is important in number theory and has practical relevance in areas such as cryptography, data validation, and algorithm design.  Algorithm / Logic Explanation To check whether a number is prime, we need to verify that it is not divisible by any number other than 1 and itself. The algorithm follows a si...

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

๐ŸŒ™