C Program To Find Grace Marks of Student Using Switch Case

Write a C 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 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 gets 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 4 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 is of 5 marks per subject.

Related Read:
if else statement in C
Switch Case Default In C Programming Language

Video Tutorial: C Program To Find Grace Marks of Student Using Switch Case


[youtube https://www.youtube.com/watch?v=wpfh-tLWbEo]

YouTube Link: https://www.youtube.com/watch?v=wpfh-tLWbEo [Watch the Video In Full Screen.]

Source Code: C Program To Find Grace Marks of Student Using Switch Case

#include<stdio.h>

int main()
{
    int grade, grace = 0, failed;

    printf("Enter the class obtained by the student\n");
    scanf("%d", &grade);

    printf("How many subjects has the student failed\n");
    scanf("%d", &failed);

    switch(grade)
    {
        case 1:
                if(failed > 3)
                    grace = 0;
                else
                    grace = 5;

                break;

        case 2:
                if(failed > 2)
                    grace = 0;
                else
                    grace = 4;

                break;

        case 3:
                if(failed > 1)
                    grace = 0;
                else
                    grace = 5;

                break;

         default: printf("You entered wrong class for the student\n");

    }

    if(grade == 1 || grade == 2 || grade == 3)
    {
        printf("The student has obtained a grace marks of %d per subject\n", 
              grace);
    }

    return 0;
}

Output 1
Enter the class obtained by the student
1
How many subjects has the student failed
2
The student has obtained a grace marks of 5 per subject

Output 2
Enter the class obtained by the student
2
How many subjects has the student failed
3
The student has obtained a grace marks of 0 per subject

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Switch Case Default In C Programming Language

In this video tutorial lets learn how switch case decision control statement works and its syntax in C Programming Language.

Related Read:
Simple Calculator Program using Switch Case: C
break Statement In C Programming Language
Continue Statement In C Programming Language

Note: The condition in switch must be a integer constant or an expression which evaluates to an integer constant.

Video Tutorial: Switch Case Default In C Programming Language


[youtube https://www.youtube.com/watch?v=XklMeJ5ha1A]

YouTube Link: https://www.youtube.com/watch?v=XklMeJ5ha1A [Watch the Video In Full Screen.]

Source Code: Switch Case Default In C Programming Language

#include<stdio.h>

int main()
{
    char choice;

    printf("Enter your choice\n");
    scanf("%c", &choice);

    switch(choice)
    {
        case 'a':
        case 'A': printf("You typed A or a\n");
                  break;

        case 'b':
        case 'B': {
                   printf("You typed B or b\n");
                   break;
                  }

        default: printf("Your selection is wrong!\n");
    }

    return 0;
}

Output 1
Enter your choice
B
You typed B or b

Output 2
Enter your choice
b
You typed B or b

Here we’ve not specified keyword break after case ‘a’ and case ‘b’, that means if user enters a or A, for both whatever is present in case ‘A’ gets executed and then control exits switch case when it encounters break statement. Similarly for ‘b’ and ‘B’.

Also note that curly braces are optional here. Infact its unnecessary to use curly braces in switch case statements.

#include<stdio.h>

int main()
{
    int choice;

    printf("Enter your choice\n");
    scanf("%d", &choice);

    switch(choice)
    {
        case 1: printf("This is first statement\n");

        case 2: printf("This is second statement\n");

        default: printf("Your selection is wrong!\n");
    }

    return 0;
}

Output 1
Enter your choice
1
This is first statement
This is second statement
Your selection is wrong!

Output 2
Enter your choice
2
This is second statement
Your selection is wrong!

Here we’re not using break after any case, so it’s printing statements present in all the cases.

#include<stdio.h>
int main()
{
    int choice;

    printf("Enter your choice\n");
    scanf("%d", &choice);

    switch(choice)
    {
        case 1: printf("This is first statement\n");
                break;

        case 2: printf("This is second statement\n");
                break;

        default: printf("Your selection is wrong!\n");
    }

    return 0;
}

Output 1
Enter your choice
1
This is first statement

Output 2
Enter your choice
2
This is second statement

Output 3
Enter your choice
a
Your selection is wrong!

#include<stdio.h>
int main()
{
    int choice;

    printf("Enter your choice\n");
    scanf("%d", &choice);

    switch(choice)
    {
        case 2: printf("Apple\n");
                break;

        case 1: printf("IBM\n");
                break;

        default: printf("Your selection is wrong!\n");
    }

    return 0;
}

Output 1
Enter your choice
1
IBM

Here order doesn’t matter. As you can see from above example, case 2 comes before case 1.

#include<stdio.h>
int main()
{
    int choice;

    printf("Enter your choice\n");
    scanf("%d", &choice);

    switch(choice)
    {
        printf("List of companies\n");
        case 2: printf("Apple\n");
                break;

        case 1: printf("IBM\n");
                break;

        default: printf("Your selection is wrong!\n");
    }

    return 0;
}

Output 1
Enter your choice
1
IBM

All the code must go inside any of the cases orelse those code will not have any effect. In above program the printf statement with “List of companies” doesn’t get printed and it doesn’t even throw any error.

Note:
1. default case is optional.
2. Value of cases in switch must always be unique.
3. switch can not be used with float, double.
4. Switch, case, default are all builtin keywords.
5. continue does not take the control back to the start of switch.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Simple Calculator Program using Switch Case: C

In this video tutorial we shall perform Addition, Subtraction, Multiplication and Division of numbers based on user input, using switch case statement(decision control statement).

Related Read:
Basic Arithmetic Operations In C
Addition of 2 Numbers: C
Subtraction of 2 Numbers: C
Multiplication of 2 Numbers: C
Division of 2 Numbers: C
else if statement in C

We had written same calculator program using else-if clause. Same program has been modified to use Switch case in this program.
Simple Calculator Application In C

Simple Calculator using Switch Case: C Program


[youtube https://www.youtube.com/watch?v=HHuaYKKoMR4]

YouTube Link: https://www.youtube.com/watch?v=HHuaYKKoMR4 [Watch the Video In Full Screen.]


Working of Calculator Program
We display list of operation one can perform. That is,

1. Addition
2. Subtraction
3. Multiplication
4. Division

We ask the user to input his / her choice for arithmetic operation. If the user selects 1, then we ask the user to enter 2 integer numbers to perform addition operation. Once the user enters 2 integer numbers we add and display the result on the screen.

If the user enters wrong choice, we ask the user to enter proper choice.

Calculator Program using Switch Case: C Program

#include < stdio.h >

int main()
{
    int a, b;
    char choice;

    printf("Enter your choice\n");
    printf("a. Addition\nb. Subtraction\nc. Multiplication\nd. Division\n");
    scanf("%c", &choice);


   printf("Enter 2 integer numbers\n");
   scanf("%d %d", &a, &b);


    switch(choice)
    {
        case 'a': printf("%d + %d = %d\n", a, b, (a+b));
                break;

        case 'b': printf("%d - %d = %d\n", a, b, (a-b));
                break;

        case 'c': printf("%d x %d = %d\n", a, b, (a*b));
                break;

        case 'd': if( b != 0)
                    printf("%d / %d = %d\n", a, b, (a/b));
                else
                    printf("Number can't be divided by 0\n");
                break;

        default: printf("You entered wrong choice\n");
                 break;
    }

    return 0;
}

Output
Enter your choice
a. Addition
b. Subtraction
c. Multiplication
d. Division
c
Enter 2 integer numbers
5
10
5 x 10 = 50

In above program we are asking user to enter character a or b or c or d to perform addition, subtraction, multiplication and division operations respectively.

#include < stdio.h >

int main()
{
    int a, b, choice;

    printf("Enter your choice\n");
    printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");
    scanf("%d", &choice);

    if( choice > 4 )
    {
        printf("Select with in the range!\n");
    }
    else
    {
        printf("Enter 2 integer numbers\n");
        scanf("%d %d", &a, &b);
    }


    switch(choice)
    {
        case 1: printf("%d + %d = %d\n", a, b, (a+b));
                break;

        case 2: printf("%d - %d = %d\n", a, b, (a-b));
                break;

        case 3: printf("%d x %d = %d\n", a, b, (a*b));
                break;

        case 4: if( b != 0)
                    printf("%d / %d = %d\n", a, b, (a/b));
                else
                    printf("Number can't be divided by 0\n");
                break;

        default: printf("You entered wrong choice\n");
                 break;
    }

    return 0;
}

Output:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division
4
Enter 2 integer numbers
10
2
10 / 2 = 5

Here we are asking user to enter 1 or 2 or 3 or 4 to choose the arithmetic operation.

#include

int main()
{
    int a, b;
    char choice;

    printf("Enter your choice\n");
    printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");
    scanf("%c", &choice);

    printf("Enter 2 integer numbers. Format: a + b\n");
    scanf("%d %c %d", &a, &choice, &b);



    switch(choice)
    {
        case '+': printf("%d + %d = %d\n", a, b, (a+b));
                break;

        case '-': printf("%d - %d = %d\n", a, b, (a-b));
                break;

        case '*': printf("%d x %d = %d\n", a, b, (a*b));
                break;

        case '/': if( b != 0)
                    printf("%d / %d = %d\n", a, b, (a/b));
                else
                    printf("Number can't be divided by 0\n");
                break;

        default: printf("You entered wrong choice\n");
                 break;
    }

    return 0;
}

Output:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division
1
Enter 2 integer numbers. Format: a + b
50 + 60
50 + 60 = 110

Here we ask the user to enter values as well as the operation to be performed. Operands and operator.

Note 1: There is no need of curly braces inside case.
Note 2: If a case doesn’t end with break statement, then the execution continues and the block of code present inside next case will also get executed.
Note 3: default case is optional. And the code inside it executes only when non of the cases match.
Note 4: Here switch, case, break, default are all keywords / reserve words.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert