Switch case

  1. Write a complete C program that  takes input from keyboard and Gives the name of day in the input of 1 to 7 and write an invalid message.

    #include<stdio.h>
    int main(){
    int day;
    printf("Enter number:");
    scanf("%d",&day);


    switch (day){
    case 1:
        printf("Sunday");
        break;
       
    case 2:
        printf("Monday");
        break;
    case 3:
        printf("Tuesday");
        break;
    case 4:
        printf("Wednesday");
        break;
    case 5:
        printf("Thursday");
        break;
       
    case 6:
        printf("Friday");
        break;
    case 7:
        printf("Saturday");
        break;


    default:
    printf("Invalid Date (Enter 1-7 only)");
        break;
    }




    return 0;
    }






  2. Write a complete C program that reads a value in the range of 1 to 12 and print the name of that month and the next month. Print error for any other input value.
    #include <stdio.h>

    int main() {
        int month;

        // Array containing the names of the months
        const char *months[] = {
            "January", "February", "March", "April", "May", "June",
            "July", "August", "September", "October", "November", "December"
        };

        // Prompt the user for input
        printf("Enter a month number (1-12): ");
        scanf("%d", &month);

        // Check if the input is within the valid range
        if(month >= 1 && month <= 12) {
            // Print the current month
            printf("Current month: %s\n", months[month - 1]);
           
            // Determine the next month (handling December)
            int nextMonth = (month % 12);
           
            // Print the next month
            printf("Next month: %s\n", months[nextMonth]);
        } else {
            // Print an error message for invalid input
            printf("Error: Invalid month number. Please enter a number between 1 and 12.\n");
        }

        return 0;
    }






  3. write a program to find the grace mark for a student using switch, The user should enter the class obtained by the student and the number of subjects he has failed in. Use the following logic.
    -If the student gets first class and the number of subjects he failed in is greater than 3 , then he does not get any grace. Otherwise the grace is of 5 marks per subject.
    -If the student get second class and the number of subjects he failed in is greater than 2, then he does not get any grace. Otherwise the grace is of 5 marks per subject.
    -If the student gets third class and the number of subjects he failed in is greater than 1, then he does not get any grace otherwise the grace of 5 marks.
    #include <stdio.h>

    int main() {
        int student_class, failed_subjects, grace_marks = 0;

        // Input the class obtained by the student
        printf("Enter the class obtained by the student (1 for First, 2 for Second, 3 for Third): ");
        scanf("%d", &student_class);

        // Input the number of subjects the student has failed in
        printf("Enter the number of subjects the student has failed in: ");
        scanf("%d", &failed_subjects);

        // Calculate grace marks using switch-case
        switch (student_class) {
            case 1: // First class
                if (failed_subjects <= 3) {
                    grace_marks = failed_subjects * 5;
                }
                break;
            case 2: // Second class
                if (failed_subjects <= 2) {
                    grace_marks = failed_subjects * 5;
                }
                break;
            case 3: // Third class
                if (failed_subjects <= 1) {
                    grace_marks = 5;
                }
                break;
            default:
                printf("Invalid class entered.\n");
                return 1; // Exit the program with an error code
        }

        // Output the grace marks
        printf("The student gets %d grace marks.\n", grace_marks);

        return 0;
    }







0 Comments had been done yet:

Post a Comment