Skip to content

luc043.c

Problem Statement

Write a program to find the grace marks 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 he fails in more than 3 subjects, he does not get any grace, Otherwise, he gets a grace of 5 marks per subject. - If the student gets second class and he fails in more than 2 subjects, he does not get any grace. Otherwise, he gets a grace of 4 marks per subject. - If the student gets third class and he fails in more than 1 subject, then he does not get any grace. Otherwise, he gets a grace of 5 marks.

Metadata

Property Detail
Author Amit Dutta amitdutta4255@gmail.com
Date 12 Dec 2025
License MIT License (See the LICENSE file for details)
Difficulty Beginner (index: 2 / 10)

Concepts

Beta Feature

This concept detection system is still in beta and may occasionally show incorrect or incomplete results.

  • Pointers
  • Recursion
  • Sorting (possible)
  • Iteration

Actions

Raw View on GitHub

You can print or save this file by opening Raw and using your browser.

Source Code

#include <stdio.h>

int main()
{
    int studentClass, failedSubjectCount, graceMarks = 0;
    printf("Class obtained by the student (Enter 1 for First Class, 2 for Second Class, 3 for Third Class): ");
    scanf("%d", &studentClass);
    printf("Failed Subject Count: ");
    scanf("%d", &failedSubjectCount);

    if (failedSubjectCount < 0) {
        printf("\nFailed subject count cannot be negative.");
        return 1;
    }

    switch (studentClass)
    {
    case 1:
        if (failedSubjectCount <= 3)
        {
            graceMarks += 5 * failedSubjectCount;
        }
        break;

    case 2:
        if (failedSubjectCount <= 2)
        {
            graceMarks += 4 * failedSubjectCount;
        }
        break;

    case 3:
        if (failedSubjectCount <= 1)
        {
            graceMarks += 5 * failedSubjectCount;
        }
        break;

    default:
        printf("\nWrong Choice.");
        return 1;
    }

    printf("\nStudent will get %d grace marks.", graceMarks);
    return 0;
}

Explanation

Explain with AI

Copy the prompt below and paste it into any AI assistant.

    You are explaining a C programming code to a beginner.

    STRICT RULES:

    - Only use the given code. Do NOT assume anything not present.

    - Do NOT add extra examples.

    - Keep explanation clear and short.

    - If something is unclear, say "Not clear from code".

    - Follow the exact format below. Do NOT change headings.

    FORMAT:

    [START]

    ## What it does

    (Explain the overall purpose in 1-2 sentences)

    ## Step-by-step

    (Explain how the code works in steps, simple language)

    ## Key Concepts

    (List concepts like loop, condition, function, etc.)

    ## Notes

    (Mention any limitations, errors, or assumptions)

    [END]

    CODE (luc043.c):

    #include <stdio.h>

    int main()
    {
        int studentClass, failedSubjectCount, graceMarks = 0;
        printf("Class obtained by the student (Enter 1 for First Class, 2 for Second Class, 3 for Third Class): ");
        scanf("%d", &studentClass);
        printf("Failed Subject Count: ");
        scanf("%d", &failedSubjectCount);

        if (failedSubjectCount < 0) {
            printf("\nFailed subject count cannot be negative.");
            return 1;
        }

        switch (studentClass)
        {
        case 1:
            if (failedSubjectCount <= 3)
            {
                graceMarks += 5 * failedSubjectCount;
            }
            break;

        case 2:
            if (failedSubjectCount <= 2)
            {
                graceMarks += 4 * failedSubjectCount;
            }
            break;

        case 3:
            if (failedSubjectCount <= 1)
            {
                graceMarks += 5 * failedSubjectCount;
            }
            break;

        default:
            printf("\nWrong Choice.");
            return 1;
        }

        printf("\nStudent will get %d grace marks.", graceMarks);
        return 0;
    }