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

C Program To Find Grade of Steel

A certain grade of steel is graded according to the following conditions:

1. Hardness must be greater than 50.
2. Carbon content must be less than 0.7
3. Tensile strength must be greater than 5600

The grades are as follows:

Grade is 10, if all three conditions are met.
Grade is 9, if conditions 1 and 2 are met.
Grade is 8, if conditions 2 and 3 are met.
Grade is 7, if conditions 1 and 3 are met.
Grade is 6, if only one condition is met.
Grade is 5, if none of the conditions are met.

Write a C program, which will require the user to give values of hardness, carbon content and tensile strength of the steel under consideration and output the grade of the steel.

Note: Tensile strength is the maximum stress that a material can withstand while being stretched or pulled before breaking.

Related Read:
else if statement in C
Relational Operators In C

Expected Output for the Input

User Input:
Enter values of hardness, tensile Strength and Carbon Content in Steel
55
5601
0.6

Output:
Steel Grade is 10

Video Tutorial: C Program To Find Grade of Steel


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

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

Source Code: C Program To Find Grade of Steel

#include<stdio.h>

int main()
{
    int hardness, ts;
    float carbon;

    printf("Enter values of hardness, tensile Strength 
            and Carbon Content in Steel\n");
    scanf("%d %d %f", &hardness, &ts, &carbon);

    if(hardness > 50 && carbon < 0.7 && ts > 5600)
    {
        printf("Steel Grade is 10\n");
    }
    else if(hardness > 50 && carbon < 0.7)
    {
        printf("Steel Grade is 9\n");
    }
    else if(carbon < 0.7 && ts > 5600)
    {
        printf("Steel Grade is 8\n");
    }
    else if(hardness > 50 && ts > 5600)
    {
        printf("Steel Grade is 7\n");
    }
    else if(hardness > 50 || carbon < 0.7 || ts > 5600)
    {
        printf("Steel Grade is 6\n");
    }
    else if(hardness < 50 && carbon > 0.7 && ts < 5600)
    {
        printf("Steel Grade is 5\n");
    }

    return 0;
}

Output 1:
Enter values of hardness, tensile Strength and Carbon Content in Steel
60
6000
0.5
Steel Grade is 10

Output 2:
Enter values of hardness, tensile Strength and Carbon Content in Steel
80
5000
0.5
Steel Grade is 9

Output 3:
Enter values of hardness, tensile Strength and Carbon Content in Steel
40
6000
0.5
Steel Grade is 8

Output 4:
Enter values of hardness, tensile Strength and Carbon Content in Steel
60
8000
0.8
Steel Grade is 7

Output 5:
Enter values of hardness, tensile Strength and Carbon Content in Steel
51
5000
0.8
Steel Grade is 6

Output 6:
Enter values of hardness, tensile Strength and Carbon Content in Steel
41
5000
0.8
Steel Grade is 5

Update To The Program

If we input:
Hardness: 50
Tensile Strength: 5600
Carbon Content: 0.7

Output will be: Steel Grade is 6.

If you check with Grade 6 condition inside if condition: ( hardness > 50 || carbon < 0.7 || ts > 5600 ) None of the condition is true. Condition for Grade 6 is: Grade is 6, if only one condition is met.

So our output is wrong. But why is it showing wrong result?

#include<stdio.h>

int main()
{
    float x = 0.7;

    printf("%d\n", sizeof(x));
    printf("%d\n", sizeof(0.7));
    printf("%d\n", sizeof(0.7f));

    return 0;
}

Output:
4
8
4

As we can clearly see 0.7 is treated as double. We can typecase it using 0.7f. So in our steel program, variable declaration(float carbon) and comparison with 0.7(which is double) is causing the bug.

With this knowledge we can rewrite our program as follows:

Source Code: C Program To Find Grade of Steel

#include<stdio.h>

int main()
{
    int hardness, ts;
    double carbon;

    printf("Enter values of hardness, tensile Strength and Carbon Content in Steel\n");
    scanf("%d%d%lf", &hardness, &ts, &carbon);

    if(hardness > 50 && carbon < 0.7 && ts > 5600)
    {
        printf("Steel Grade is 10\n");
    }
    else if(hardness > 50 && carbon < 0.7)
    {
        printf("Steel Grade is 9\n");
    }
    else if(carbon < 0.7 && ts > 5600)
    {
        printf("Steel Grade is 8\n");
    }
    else if(hardness > 50 && ts > 5600)
    {
        printf("Steel Grade is 7\n");
    }
    else if(hardness > 50 || carbon < 0.7 || ts > 5600)
    {
        printf("Steel Grade is 6\n");
    }
    else if(hardness < 50 && carbon > 0.7 && ts < 5600)
    {
        printf("Steel Grade is 5\n");
    }
    else
    {
        printf("Did not match any criteria\n");
    }

    return 0;
}

Output 1:
Enter values of hardness, tensile Strength and Carbon Content in Steel
50
5600
0.7
Did not match any criteria

Output 2:
Enter values of hardness, tensile Strength and Carbon Content in Steel
41
5000
0.8
Steel Grade is 5

In above program we’re declaring carbon as double and using %lf format specifier to take value of carbon as input from the user.

Typecasing 0.7 to floating point value

#include<stdio.h>

int main()
{
    int hardness, ts;
    float carbon;

    printf("Enter values of hardness, tensile Strength and Carbon Content in Steel\n");
    scanf("%d%d%f", &hardness, &ts, &carbon);

    if(hardness > 50 && carbon < 0.7f && ts > 5600)
    {
        printf("Steel Grade is 10\n");
    }
    else if(hardness > 50 && carbon < 0.7f)
    {
        printf("Steel Grade is 9\n");
    }
    else if(carbon < 0.7f && ts > 5600)
    {
        printf("Steel Grade is 8\n");
    }
    else if(hardness > 50 && ts > 5600)
    {
        printf("Steel Grade is 7\n");
    }
    else if(hardness > 50 || carbon < 0.7f || ts > 5600)
    {
        printf("Steel Grade is 6\n");
    }
    else if(hardness < 50 && carbon > 0.7f && ts < 5600)
    {
        printf("Steel Grade is 5\n");
    }
    else
    {
        printf("Did not match any criteria\n");
    }

    return 0;
}

Output 1:
Enter values of hardness, tensile Strength and Carbon Content in Steel
50
5600
0.7
Did not match any criteria

Output 2:
Enter values of hardness, tensile Strength and Carbon Content in Steel
41
5000
0.8
Steel Grade is 5

Here we are typecasing 0.7 into floating point value by using 0.7f in else if conditions, which matches with the data type of variable carbon.

Related Read:
Comparing Floating Point Variable With a Value In C Programming

Video Tutorial: Comparing Floating Point Variable With a Value In C Programming


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

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

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

Nested if else Statement In C

In this program we’ll show you nesting of if else statements. Here we are illustrating the concept by taking score/marks of 5 subject from the user. Then we calculate the percentage of it and display grade to the user.

Simple Logic

When the condition in if statement is false then only else block gets executed. In our program, we check, if percentage is greater than or equal to 60. If it is false, then only control shifts to else block. So inside else(nested if else) we need not once again check if the percentage is less than 60.

Note: We are not using else if and logical operator in this C program purposefully. We want to show nesting of if else in this program.

 
#include < stdio.h >

int main()
{
    float s1, s2, s3, s4, s5, per;

    printf("Enter marks of 5 subject\n");
    scanf("%f %f %f %f %f", &s1, &s2, &s3, &s4, &s5);

    per = (s1 + s2 + s3 + s4 + s5) / 5.0;

    if(per >= 60)
    {
        printf("Grade A\n");
    }
    else
    {
        if(per >= 50)
        {
            printf("Grade B\n");
        }
        else
        {
            if(per >= 40)
            {
                printf("Grade C\n");
            }
            else
            {
                printf("Failed!\n");
            }
        }
    }

    return 0;
}

Output 1:
Enter marks of 5 subject
59
60
62
61
63
Grade A

Output 2:
Enter marks of 5 subject
59
55
56
53
58
Grade B

Output 3:
Enter marks of 5 subject
49
45
46
49
48
Grade C

Output 4:
Enter marks of 5 subject
39
45
25
36
35
Failed!

Student Grade Calculation using Nested if else: C Program


[youtube https://www.youtube.com/watch?v=I-EHRu-Nq8o]

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


Note: In next video we’ll show you how to perform the same operation as shown in above C program, using else-if and logical operators.

Also check the program C Program To Check Leap Year for a perfect nested if else example.

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