C Program To Determine Designation of Employee Based On Salary

Rewrite the following C program using Conditional / Ternary Operator.

Source Code: C Program To Determine Designation of Employee Based On Salary Using nested if else condition

#include<stdio.h>

int main()
{
    float sal;

    printf("Enter the salary\n");
    scanf("%f", &sal);

    if(sal >= 25000 && sal <= 40000)
    {
        printf("Manager\n");
    }
    else
        if(sal >= 15000 && sal < 25000)
            printf("Accountant\n");
        else
            printf("Clerk\n");

    return 0;
}

Note: We need to rewrite the above C program using Ternary / Conditional Operator.

Good Example of Nested Ternary Operator
C Program To Check Leap Year Using Ternary Operator

Related Read:
Nested if else Statement In C
Ternary Operator / Conditional Operator In C
Relational Operators In C
Logical Operators In C

Ternary / Conditional Operator General Form

(expression_1) ? (expression_2) : (expression_3);

expression_1 is a comparison/conditional argument. expression_2 is executed/returned if expression_1 results in true, expression_3 gets executed/returned if expression_1 is false.

Ternary operator / Conditional Operator can be assumed to be shortened way of writing an if-else statement.

Expected Output for the Input

User Input:
Enter the salary
40000

Output:
Manager

Logic To Convert from if else To Conditional Operator

As you can see we’ve nested if else statement inside first else statement. So we’ll have nested conditional operator inside expression_3 of outer conditional operator.

Video Tutorial: C Program To Determine Designation of Employee Based On Salary


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

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

Source Code: C Program To Determine Designation of Employee Based On Salary using Conditional Operator

#include<stdio.h>

int main()
{
    int sal;

    printf("Enter the salary\n");
    scanf("%d", &sal);

    ( sal >= 25000 && sal <= 40000 ) ?
    printf("Manager\n") :
    ( sal >= 15000 && sal <  25000)?
    printf("Accountant\n") :
    printf("Clerk\n");

    return 0;
}

Output 1:
Enter the salary
35000
Manager

Output 2:
Enter the salary
20000
Accountant

Output 3:
Enter the salary
10000
Clerk

Note: As you can see if you enter the salary as $40001 and above, it shows the designation as Clerk. That is because we’re not checking for salary above $40000. With the problem statement given above, we assume that this C program is for employees with salary less than $40000 and for designation Manager, Accountant and Clerk only.

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 Sum of Square of Sine and Cosine of an Angle is 1

Write a C program to receive value of an angle in degrees and check whether sum of squares of sine and cosine of this angle is equal to 1.

Related Read:
Basic Arithmetic Operations In C
Calculate Power of a Number using pow(): C Program

Formula To Convert Angle From Degree To Radian

radian = degree x ( PI / 180.0)
where PI is 3.14159265359

Note: In C programming Language, we’ve a builtin constant M_PI which has value of PI.

Expected Output for the Input

User Input:
Enter angle in Degree
45

Output:
Sum of square of sin(45.00) and cos(45.00) is 1

Logic To Check If Sum of Square of Sine and Cosine of an Angle is 1

User enters the angle in degree. We convert angle from degree to radian.
radian = degree x ( PI / 180.0)
Next we use pow() method present in math.h library to sqaure the values of sin(angle) and cos(angle). We add the square of sin(angle) and cos(angle) and store it inside the variable sum, and display appropriate message to the user.

Video Tutorial: C Program To Check If Sum of Square of Sine and Cosine of an Angle is 1


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

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

Source Code: C Program To Check If Sum of Square of Sine and Cosine of an Angle is 1

#include<stdio.h>
#include<math.h>

int main()
{
    float sum = 0, angle, temp;

    printf("Enter angle in Degree\n");
    scanf("%f", &angle);

    temp   = angle;

    angle  = angle * ( M_PI / 180.0 );

    sum    = (  pow(sin(angle), 2) + pow(cos(angle), 2)  );

    if(sum == 1)
    {
        printf("Sum of square of sin(%0.2f) and cos(%0.2f) is 1\n", temp, temp);
    }
    else
    {
        printf("Sum of square of sin(%0.2f) and cos(%0.2f) is not 1\n", temp, temp);
    }

    return 0;
}

Output 1:
Enter angle in Degree
30
Sum of square of sin(30.00) and cos(30.00) is 1

Output 2:
Enter angle in Degree
45
Sum of square of sin(45.00) and cos(45.00) is 1

Output 3:
Enter angle in Degree
60
Sum of square of sin(60.00) and cos(60.00) is 1

Output 4:
Enter angle in Degree
75
Sum of square of sin(75.00) and cos(75.00) is 1

Output 5:
Enter angle in Degree
90
Sum of square of sin(90.00) and cos(90.00) is 1

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 Special Symbol or Not using Conditional Operator

Using Conditional / Ternary Operator determine, whether a character entered through the keyboard is a Special Symbol or not.

Note: ASCII values start from 0 to 255. So we have total of 266 ASCII values.

ASCII Code for Special Symbols
0 – 47
58 – 64
91 – 96
123 – 255

Also Check:
C Program To Find Lowercase Alphabet or Not using Conditional Operator

Related Read:
Relational Operators In C
Logical Operators In C
Ternary Operator / Conditional Operator In C
C Program To Print All ASCII Characters and Code

Ternary / Conditional Operator General Form

(expression_1) ? (expression_2) : (expression_3);

expression_1 is a comparison/conditional argument. expression_2 is executed/returned if expression_1 results in true, expression_3 gets executed/returned if expression_1 is false.

Ternary operator / Conditional Operator can be assumed to be shortened way of writing an if-else statement.

Expected Output for the Input

User Input:
Enter a Character
$

Output:
Character Entered Is a Special Symbol

Logic To Find Special Symbol or Not using Conditional Operator

Using Conditional Operator we write the condition, if user entered character is in between or equal to ASCII values 0 – 47 or 58 to 64 or 91 to 96 or greater than or equal to 123. We use Relational Operator and Logical Operators to accomplish the task.

If the condition in expression_1 is true, then whatever code is present in expression_2 gets executed. If condition is expression_1 is false then the code present in expression_3 gets executed.

Video Tutorial: C Program To Find Character is Special Symbol or Not using Conditional Operator


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

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

Source Code: C Program To Find Special Symbol or Not using Conditional Operator

#include<stdio.h>

int main()
{
    char ch;

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

    ( (ch >= 0  && ch <= 47) ||
      (ch >= 58 && ch <= 64) ||
      (ch >= 91 && ch <= 96) ||
      (ch >= 123) ) ?
      printf("Character Entered Is a Special Symbol\n") :
      printf("Character Entered Is not a Special Symbol\n");

    return 0;
}

Output 1:
Enter a Character
#
Character Entered Is a Special Symbol

Output 2:
Enter a Character
a
Character Entered Is not a Special Symbol

Output 3:
Enter a Character
Z
Character Entered Is not a Special Symbol

Output 4:
Enter a Character
5
Character Entered Is not a Special Symbol

Output 5:
Enter a Character
$
Character Entered Is a Special Symbol

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 Lowercase Alphabet or Not using Conditional Operator

Using Conditional Operator / Ternary Operator determine, whether the character entered through the keyboard is a lower case English alphabet or not.

Also Check:
C Program To Find Special Symbol or Not using Conditional Operator

Related Read:
Relational Operators In C
Logical Operators In C
Ternary Operator / Conditional Operator In C
C Program To Print All ASCII Characters and Code

Expected Output for the Input

User Input:
Enter a character
a

Output:
Character entered is a lowercase English alphabet

Logic To Find Lowercase Alphabet or Not using Conditional Operator

Using Conditional Operator we write the condition, if user entered character is greater than or equal to ASCII Value 97(which corresponds to lowercase character a) and less than or equal to ASCII Value 122(which corresponds to lowercase character z).

If the condition is true, then user entered character is lower case English alphabet, if not, then its not a lowercase English alphabet.

Video Tutorial: C Program To Find Lowercase Alphabet or Not using Conditional Operator


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

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

Source Code: C Program To Find Lowercase Alphabet or Not using Conditional Operator

#include < stdio.h >

int main()
{
    char ch;

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

    (ch >= 97 && ch <= 122) ?
    printf("Character entered is a lowercase English alphabet\n") :
    printf("Character entered is not a lowercase English alphabet\n");

    return 0;
}

Output 1:
Enter a character
$
Character entered is not a lowercase English alphabet

Output 2:
Enter a character
A
Character entered is not a lowercase English alphabet

Output 3:
Enter a character
5
Character entered is not a lowercase English alphabet

Output 4:
Enter a character
a
Character entered is a lowercase English alphabet

Output 5:
Enter a character
z
Character entered is a lowercase English alphabet

ASCII Values of Lowercase English Alphabets

ASCII value of a is 97

ASCII value of b is 98

ASCII value of c is 99

ASCII value of d is 100

ASCII value of e is 101

ASCII value of f is 102

ASCII value of g is 103

ASCII value of h is 104

ASCII value of i is 105

ASCII value of j is 106

ASCII value of k is 107

ASCII value of l is 108

ASCII value of m is 109

ASCII value of n is 110

ASCII value of o is 111

ASCII value of p is 112

ASCII value of q is 113

ASCII value of r is 114

ASCII value of s is 115

ASCII value of t is 116

ASCII value of u is 117

ASCII value of v is 118

ASCII value of w is 119

ASCII value of x is 120

ASCII value of y is 121

ASCII value of z is 122

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 Calculate BMI and BMI Category

The Body Mass Index (BMI) is defined as ratio of the weight of a person(in kilograms) to the square of the height(in meters). Write a C program that receives weight and height, calculates the BMI, and reports the BMI category as per the following table:

BMI CategoryBMI
Starvation < 15
Anorexic15.1 to 17.5
Underweight17.6 to 18.5
Ideal18.6 to 24.9
Overweight25 to 25.9
Obese30 to 30.9
Morbidly Obese>= 40

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

Formula To Calculate Body Mass Index (BMI)

bmi = weight / (height x height);

Logic To Calculate BMI and BMI Category

We ask the user to enter weight in kelogram(kg) and height in meters(m). Then using above formula we calculate BMI(Body Mass Index). Next, by referring to the chart below we display the BMI category.

BMI Category

For BMI Category Ideal:
bmi >= 18.6 && bmi <= 24.9

Expected Output for the Input

User Input:
Enter height in meter
1.4
Enter weight in kg
60

Output:
Your Body Mass Index(BMI) is 30.612246
Your BMI category is: Obese

Video Tutorial: C Program To Calculate BMI and BMI Category


[youtube https://www.youtube.com/watch?v=Lz_o-l5vNwA]

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

Source Code: C Program To Calculate BMI and BMI Category

#include < stdio.h >
int main()
{
    float height, weight, bmi;

    printf("Enter height in meter\n");
    scanf("%f", &height);

    printf("Enter weight in kg\n");
    scanf("%f", &weight);

    bmi = weight / (height * height);

    printf("Your Body Mass Index(BMI) is %f\n", bmi);

    if(bmi < 15)
    {
        printf("Your BMI category is: Starvation\n");
    }
    else if(bmi >= 15.1 && bmi <= 17.5)
    {
        printf("Your BMI category is: Anorexic\n");
    }
    else if(bmi >= 17.6 && bmi <= 18.5)
    {
        printf("Your BMI category is: Underweight\n");
    }
    else if(bmi >= 18.6 && bmi <= 24.9)
    {
        printf("Your BMI category is: Ideal\n");
    }
    else if(bmi >= 25 && bmi <= 25.9)
    {
        printf("Your BMI category is: Overweight\n");
    }
    else if(bmi >= 30 && bmi <= 30.9)
    {
        printf("Your BMI category is: Obese\n");
    }
    else if(bmi >= 40)
    {
        printf("Your BMI category is: Morbidly Obese\n");
    }
    else
    {
        printf("Wrong entry\n");
    }

    return 0;
}

Output 1:
Enter height in meter
0.8
Enter weight in kg
80
Your Body Mass Index(BMI) is 125.000000
Your BMI category is: Morbidly Obese

Output 2:
Enter height in meter
1.4
Enter weight in kg
50
Your Body Mass Index(BMI) is 25.510204
Your BMI category is: Overweight

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