C Program To Determine Weight Class of a Boxer

In boxing the weight class of a boxer is decided as per the following table. Write a C program that receives weight as input and prints out the boxer’s weight class.

Boxer Class Weight In Pounds
Flyweight < 115
Bantamweight 115 – 121
Featherweight 122 – 153
Middleweight 154 – 189
Heavyweight >= 190

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

Expected Output for the Input

User Input:
Enter boxers weight in pounds
500

Output:
Boxer is of weight class Heavyweight

Video Tutorial: C Program To Determine Weight Class of a Boxer


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

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

Source Code: C Program To Determine Weight Class of a Boxer

#include < stdio.h >

int main()
{
    int wt;

    printf("Enter boxers weight in pounds\n");
    scanf("%d", &wt);

    if(wt < 115)
    {
        printf("Boxer is of weight class Flyweight\n");
    }
    else if(wt >= 115 && wt <= 121)
    {
        printf("Boxer is of weight class Bantamweight\n");
    }
    else if(wt >= 122 && wt <= 153)
    {
        printf("Boxer is of weight class Featherweight\n");
    }
    else if(wt >= 154 && wt <= 189)
    {
        printf("Boxer is of weight class Middleweight\n");
    }
    else if(wt >= 190)
    {
        printf("Boxer is of weight class Heavyweight\n");
    }

    return 0;
}

Output 1:
Enter boxers weight in pounds
108
Boxer is of weight class Flyweight

Output 2:
Enter boxers weight in pounds
120
Boxer is of weight class Bantamweight

Output 3:
Enter boxers weight in pounds
140
Boxer is of weight class Featherweight

Output 4:
Enter boxers weight in pounds
180
Boxer is of weight class Middleweight

Output 5:
Enter boxers weight in pounds
200
Boxer is of weight class Heavyweight

boxers weight class

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

C Program To Check For Alphabet, Number and Special Symbol

Any character is entered through the keyboard, write a C program to determine whether the character entered is a capital letter, a small case letter, a digit or a special symbol.

The following table shows the range of ASCII values for various characters:
Character A – Z : ASCII Value 65 – 90
Character a – z : ASCII Value 97 – 122
Character 0 – 9 : ASCII Value 48 – 57
Special Symbol : ASCII Value 0 – 47, 58 – 64, 91 – 96, 123 – 127

ascii codes

Related Read:
else if statement in C
Relational Operators In C
C Program To Print All ASCII Characters and Code

Expected Output for the Input

User Input:
Enter a Character
$

Output:
$ is a Special Character

Video Tutorial: C Program To Check For Alphabet, Number or Special Symbol


[youtube https://www.youtube.com/watch?v=NBcG-r0P9P8]

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

Source Code: C Program To Check For Alphabet, Number and Special Symbol

#include<stdio.h>

int main()
{
    char ch;

    printf("Enter a Character\n");
    scanf("%c", &ch);

    if(ch >= 65 && ch <= 90)
    {
        printf("%c is an Uppercase Alphabet\n", ch);
    }
    else if(ch >= 97 && ch <= 122)
    {
        printf("%c is an lowercase Alphabet\n", ch);
    }
    else if(ch >= 48 && ch <= 57)
    {
        printf("%c is a Number\n", ch);
    }
    else if( (ch >= 0  && ch <= 47) ||
             (ch >= 58 && ch <= 64) ||
             (ch >= 91 && ch <= 96) ||
             (ch >= 123 && ch <= 127))
    {
        printf("%c is a Special Character\n", ch);
    }

    return 0;
}

Output 1:
Enter a Character
A
A is an Uppercase Alphabet

Output 2:
Enter a Character
i
i is an lowercase Alphabet

Output 3:
Enter a Character
8
8 is a Number

Output 4:
Enter a Character
$
$ is a Special Character

Logic To Check For Alphabet, Number and Special Symbol

We use &&(AND) operator check check for range. i.e., for number, we check from the range 48 to 57. To check if the user entered character lies in this range we use (ch >= 48 && ch <= 57). To check for multiple ranges we use ||(OR) operator. For example, for special symbol:


(ch >= 0 && ch <= 47) ||
(ch >= 58 && ch <= 64) ||
(ch >= 91 && ch <= 96) ||
(ch >= 123 && ch <= 127)

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 Check In Which Quadrant The Point Lies

Lets write a C program to determine the position of point(x,y) on the graph of x and y axis.

Important: Always remember that, to specify a point, we always write x-axis value first and then the y-axis value. i.e., (x, y)

Related Read:
C Program To Check If Point Lies on x-axis or y-axis or Origin

Logic To Check The Position of the Point in the graph
We check for 9 conditions to determine the position of the user entered point in the graph.
x and y axis graph
1. Point lies on (0, 0): Origin
2. y = 0 and x > 0. i.e., x is positive: point lies on positive side of x-axis.
3. x = 0 and y > 0. i.e., y is positive: point lies on positive side of y-axis.
4. y = 0 and x < 0. i.e., x is negative: point lies on negative side of x-axis.
5. x = 0 and y < 0. i.e., y is negative: point lies on negative side of y-axis.
6. x > 0 and y > 0. i.e., both x and y are positive: point lies in First Quadrant.
7. x < 0 and y > 0. i.e., x is negative and y is positive: point lies in Second Quadrant.
8. x < 0 and y < 0. i.e., both x and y are negative: point lies in Third Quadrant.
9. x > 0 and y < 0. i.e., x is positive and y is negative: point lies in Forth Quadrant.

Expected Output for the Input

User Input:
Enter the point(x, y)
5
5

Output:
Point lies in First Quadrant

Video Tutorial: C Program To Check In Which Quadrant The Point Lies


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

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

Source Code: C Program To Check In Which Quadrant The Point Lies

#include < stdio.h >

int main()
{
    float x, y;

    printf("Enter the point(x, y)\n");
    scanf("%f%f", &x, &y);

    if(x == 0 && y == 0)
    {
        printf("Point lies on the Origin\n");
    }
    else if(y == 0 && x > 0)
    {
        printf("Point lies on positive x-axis\n");
    }
    else if(x == 0 && y > 0)
    {
        printf("Point lies on positive y-axis\n");
    }
    else if(y == 0 && x < 0)
    {
        printf("Point lies on negative x-axis\n");
    }
    else if(x == 0 && y < 0)
    {
        printf("Point lies on negative y-axis\n");
    }
    else if(x > 0 && y > 0)
    {
        printf("Point lies in First Quadrant\n");
    }
    else if(x < 0 && y > 0)
    {
        printf("Point lies in Second Quadrant\n");
    }
    else if(x < 0 && y < 0)
    {
        printf("Point lies in Third Quadrant\n");
    }
    else if(x > 0 && y <0)
    {
        printf("Point lies in Forth Quadrant\n");
    }

    return 0;
}

Output 1:
Enter the point(x, y)
0
0
Point lies on the Origin

Output 2:
Enter the point(x, y)
5
0
Point lies on positive x-axis

Output 3:
Enter the point(x, y)
-5
0
Point lies on negative x-axis

Output 4:
Enter the point(x, y)
0
5
Point lies on positive y-axis

Output 5:
Enter the point(x, y)
0
-5
Point lies on negative y-axis

Output 6:
Enter the point(x, y)
5
5
Point lies in First Quadrant

Output 7:
Enter the point(x, y)
-5
5
Point lies in Second Quadrant

Output 8:
Enter the point(x, y)
-5
-5
Point lies in Third Quadrant

Output 9:
Enter the point(x, y)
5
-5
Point lies in Forth Quadrant

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 Check If Point Lies on x-axis or y-axis or Origin

Given a point (x, y), write a C program to find out if it lies on the x-axis, y-axis or on the origin(0, 0).

Important: Always remember that, to specify a point, we always write x-axis value first and then the y-axis value. i.e., (x, y)

Logic To Check If Point(x, y) Lies on x-axis or y-axis or Origin
In point (x, y), if x = 0 and y = 0, then the point lies on the origin. If value of x is zero and y is greater than zero, then the point lies on y-axis. If y is zero and x is greater than zero, then the point lies on x-axis.

User Input:
Enter the point(x, y)
0
5

Output:
Point lies on y-axis

Video Tutorial: C Program To Check If Point Lies on x-axis or y-axis or Origin


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

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

Source Code: C Program To Check If Point Lies on x-axis or y-axis or Origin

#include < stdio.h >

int main()
{
    float x, y;

    printf("Enter the point(x, y)\n");
    scanf("%f%f", &x, &y);

    if(x == 0 && y == 0)
    {
        printf("Point lies on the Origin\n");
    }
    else if(x == 0)
    {
        printf("Point lies on y-axis\n");
    }
    else if(y == 0)
    {
        printf("Point lies on x-axis\n");
    }
    else
    {
        printf("Point neither lies on x-axis nor on y-axis\n");
    }

    return 0;
}

Output 1:
Enter the point(x, y)
0
0
Point lies on the Origin

Output 2:
Enter the point(x, y)
5
0
Point lies on x-axis

Output 3:
Enter the point(x, y)
0
5
Point lies on y-axis

Output 4:
Enter the point(x, y)
5
5
Point neither lies on x-axis nor on y-axis

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