PART A · PRESCRIBED EXERCISE 05

Display a grade using switch

Understand the algorithm. Run the code.
Explain the result with confidence.

Read the programOpen the playground
Aim & definitionStep-by-step algorithmWorked sampleExtra test cases3 viva answers

LAB A05 · 01

Understand the task.

Aim: Display a grade using switch.

DEFINITION

Switch selection. A switch statement selects a branch using an integer-valued expression and matching case labels. Several labels can share one block of statements.

Review the related theory in Unit 2.

LAB A05 · 02

Know what goes in and what comes out.

One whole-number percentage from 0 to 100. Practice bands match Unit 2: A = 80–100, B = 60–79, C = 40–59, F = 0–39. These are example bands, not a college grading policy.

Sample input

75

Expected output

Grade = B

Source programs read the documented input directly, without prompt text, so the same sample works in the embedded compiler’s STDIN field.

LAB A05 · 03

Plan the steps before writing code.

  1. Read percentage and reject values outside 0–100.
  2. Compute percentage / 10 using integer division.
  3. Group quotient cases 8, 9 and 10 as A; 6 and 7 as B; 4 and 5 as C.
  4. Use default for F. End each completed branch with break.
  5. Display the grade.

LAB A05 · 04

One complete program. Ready to run.

Display a grade using switchmain.c
#include <stdio.h>
int main(void)
{
    int percentage;
    char grade;
    if (scanf("%d", &percentage) != 1 || percentage < 0 || percentage > 100) {
        puts("Enter a whole-number percentage from 0 to 100.");
        return 1;
    }
    switch (percentage / 10) {
        case 10:
        case 9:
        case 8: grade = 'A'; break;
        case 7:
        case 6: grade = 'B'; break;
        case 5:
        case 4: grade = 'C'; break;
        default: grade = 'F';
    }
    printf("Grade = %c\n", grade);
    return 0;
}
EXPECTED OUTPUTGrade = B

Try this programDownload A05-grade-switch.c
Compile on your own computer
gcc A05-grade-switch.c -o program

Run the generated program and enter the sample input. The examples use standard C90-compatible syntax and explicit function declarations. For these learning programs, enter numeric values representable in the stated C types and within the documented ranges.

LAB A05 · 05

Connect each statement to its purpose.

  • Dividing by 10 maps the input to a manageable set of integer case values.
  • 100 / 10 is 10, so case 10 must be included with A.
  • Stacked case labels deliberately share a branch; break prevents continuing into later grade assignments.
  • This example accepts integer percentages. Decimal percentages require a separately specified conversion or grading approach.

LAB A05 · 06

Follow the sample calculation.

Step or componentWhat happens
Input75
Switch value75 / 10 = 7
Matching branchcase 7 shares the B assignment with case 6.
ResultGrade = B

LAB A05 · 07

Check more than one input.

Try this input or caseExpected behaviour
39 / 40F / C
59 / 60C / B
79 / 80 / 100B / A / A

Time and storage

O(1) time and O(1) extra space.

Start with the sample, then test a boundary and a case that changes the result. A correct sample alone does not establish that every input is handled correctly.

LAB A05 · 08

Use the real C compiler.

Choose an example and load it into the embedded C compiler. Select I/O inside the compiler, check the sample STDIN, and press Run. Change the input or edit the source to explore a different case.

Sample input (STDIN)

75

Expected output

Grade = B

Load the compiler when you’re ready to practise.

Your C workspace

Edit real C code and see real compiler output.

Compiler provided by OneCompiler. Opening the editor connects to that service; code and input you run are processed there. If the embed is unavailable, open the C compiler in a new tab and paste the source below.

View / copy the selected source
#include <stdio.h>
int main(void)
{
    int percentage;
    char grade;
    if (scanf("%d", &percentage) != 1 || percentage < 0 || percentage > 100) {
        puts("Enter a whole-number percentage from 0 to 100.");
        return 1;
    }
    switch (percentage / 10) {
        case 10:
        case 9:
        case 8: grade = 'A'; break;
        case 7:
        case 6: grade = 'B'; break;
        case 5:
        case 4: grade = 'C'; break;
        default: grade = 'F';
    }
    printf("Grade = %c\n", grade);
    return 0;
}

LAB A05 · 09

Explain why the program works.

Why is case 10 necessary?

A percentage of 100 produces quotient 10, while 80–99 produce 8 or 9.

What does break do?

It exits the switch after the selected grade assignment.

Can a standard C case label be a range such as 80–100?

No. Ordinary standard C case labels use integer constant expressions; this program groups individual quotient labels.

LAB A05 · 10

Build the next skill.

Read the viva answers, try the extra cases, and return to the lab index to mark this exercise as practised.