Chapter 7 - Let Us C

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.


#include <stdio.h>
    unsigned short int fail,class,marks;
    printf("1. First Class\n2. Second Class\n3. Third Class\nEnter your class : ");
    scanf("%hu",&class);
    printf("Enter the number of subjects failed : ");
    scanf("%hu",&fail);

    switch(class){
        case 1:
         marks = (fail>3)?0:fail*5;
         printf("Total grace marks given = %hu",marks);
         break;
        case 2:
         marks = (fail>2)?0:fail*4;
         printf("Total grace marks given = %hu",marks);
         break;
        case 3:
         marks = (fail>1)?0:fail*5;
         printf("Total grace marks given = %hu",marks);
         break;
        default:
         printf("You entered the wrong class\n");
    }
    return 0;
}
    
    
Step 1: Include the `stdio.h` header file for standard input/output functions like `printf` and `scanf`.
Step 2: The `main` function is the entry point of the program.
Step 3: Declare unsigned short integer variables `fail`, `class`, and `marks`. Using `unsigned short int` is appropriate here because these values are non-negative and likely to be within a limited range, saving memory.
Step 4: Prompt the user to enter their class (1, 2, or 3) and store it in the `class` variable using `scanf`.
Step 5: Prompt the user to enter the number of subjects failed and store it in the `fail` variable using `scanf`.
Step 6: A `switch` statement is used to determine the grace marks based on the entered `class`.
Step 7: Inside the `switch` statement:
  • Case 1 (First Class):
    • Use a ternary operator to calculate the grace marks: `marks = (fail > 3) ? 0 : fail * 5;`. If the number of failed subjects is greater than 3, no grace marks are given (marks = 0); otherwise, grace marks are calculated as `fail * 5`.
    • Print the total grace marks given using `printf`.
    • `break` is used to exit the `switch` statement after processing this case.
  • Case 2 (Second Class):
    • Use a ternary operator to calculate the grace marks: `marks = (fail > 2) ? 0 : fail * 4;`. If the number of failed subjects is greater than 2, no grace marks are given; otherwise, grace marks are calculated as `fail * 4`.
    • Print the total grace marks given using `printf`.
    • `break` is used to exit the `switch` statement.
  • Case 3 (Third Class):
    • Use a ternary operator to calculate the grace marks: `marks = (fail > 1) ? 0 : fail * 5;`. If the number of failed subjects is greater than 1, no grace marks are given; otherwise, grace marks are calculated as `fail * 5`.
    • Print the total grace marks given using `printf`.
    • `break` is used to exit the `switch` statement.
  • Default:
    • If the entered `class` is not 1, 2, or 3, print an error message: "You entered the wrong class\n".
Step 8: The program returns 0, indicating successful execution.

Comments

Popular Posts