C Program To Calculate Overtime Pay of 10 Employees

Write a C program to calculate overtime pay of 10 employees. Overtime is paid at the rate of Rs 12 per hour for every hour worked above 40 hours. Assume that employees do not work for fractional part of an hour.

Related Read:
while loop in C programming
if else statement in C
Relational Operators In C

Logic To Calculate Overtime Pay of 10 Employees

We ask the user to enter the number of hours each employee has worked. Then we calculate overtime worked by each employee. 40 hours is considered normal work hours. Whatever number of hours worked above 40 hours will be considered for Overtime Pay. We calculate the Overtime worked and then for every hour we pay Rs 12 as Overtime pay.

Video Tutorial: C Program To Calculate Overtime Pay of 10 Employees


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

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

Source Code: C Program To Calculate Overtime Pay of 10 Employees

#include<stdio.h>

int main()
{
    int hours, count = 1, ot = 0;

    while(count <= 10)
    {
        printf("\nEnter no of hours employee %d has worked\n", count);
        scanf("%d", &hours);

        if(hours > 40)
        {
            ot = ot + (hours - 40);

            printf("Employee %d has worked %d hours\n", count, hours);
            printf("Overtime pay is Rs %d\n", (hours-40)*12);
        }
        else
        {
            printf("no of hours worked is %d, which is less than 40 hours, so no over time pay for employee %d\n", hours, count);
        }
        count++;
    }

    printf("\nTotal Overtime pay is Rs %d\n", (ot*12));

    return 0;
}

Output:

Enter no of hours employee 6 has worked
45
Employee 6 has worked 45 hours
Overtime pay is Rs 60

Enter no of hours employee 7 has worked
39
no of hours worked is 39, which is less than 40 hours, so no over time pay for employee 7

Enter no of hours employee 8 has worked
41
Employee 8 has worked 41 hours
Overtime pay is Rs 12

Enter no of hours employee 9 has worked
42
Employee 9 has worked 42 hours
Overtime pay is Rs 24

Enter no of hours employee 10 has worked
43
Employee 10 has worked 43 hours
Overtime pay is Rs 36

Total Overtime pay is Rs 252

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 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

DA, HRA, Gross Salary: C Program

Ramesh’s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary.

Related Read:
C Program to Calculate Gross Salary of an Employee

Basic Salary, DA, HRA, Gross Salary: C Program


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

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


Source Code: Basic Salary, DA, HRA, Gross Salary: C Program

#include < stdio.h >

int main()
{
    float bs, hra, da, gs;

    printf("Enter basic salary\n");
    scanf("%f", &bs);

    hra = bs * (20/100.00);
    da  = bs * (40/100.00);

    gs  = bs + hra + da;

    printf("Gross Salary = %f\n", gs);

    return 0;
}

Output 1:
Enter basic salary
10000
Gross Salary = 16000.000000

Output 2:
Enter basic salary
20000
Gross Salary = 32000.000000

#include < stdio.h >

int main()
{
    float bs, hra, da, gs;

    printf("Enter basic salary\n");
    scanf("%f", &bs);

    hra = bs * (0.2);
    da  = bs * (0.4);

    gs  = bs + hra + da;

    printf("Gross Salary = %f\n", gs);

    return 0;
}

Output 1:
Enter basic salary
10000
Gross Salary = 16000.000000

Output 2:
Enter basic salary
20000
Gross Salary = 32000.000000

Wrong division, while calculating HRA and DA

#include < stdio.h >

int main()
{
    float bs, hra, da, gs;

    printf("Enter basic salary\n");
    scanf("%f", &bs);

    hra = bs * (20/100);
    da  = bs * (40/100);

    gs  = bs + hra + da;

    printf("Gross Salary = %f\n", gs);

    return 0;
}

Output 1:
Enter basic salary
10000
Gross Salary = 10000.000000

Output 2:
Enter basic salary
20000
Gross Salary = 20000.000000

You can see that the answer is wrong. This is because we are dividing 20 and 40 by an integer number i.e., 100, so it returns only the integer part of the result which is 0. So 0 multiplied by anything will be 0. So HRA and DA will be 0. So basic salary and gross salary will be shown equal in that case, which is wrong result.

Formula to calculate Gross Salary is:
Gross Salary = Basic Salary + Dearness allowance + House Rent Allowance.

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 Gross Salary of an Employee

In a company an employee is paid as under: If his basic salary is less than 5000 then he’ll get 10% of his base salary as HRA and 90% of his base salary as DA. If his basic salary is above 5000, then he’ll get 600 HRA and 95% of his base salary as DA.

HRA – House Rent Allowance.
DA – Dearness Allowance.

C Program to Calculate Gross Salary of an Employee


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

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


Source Code: Calculate Gross Salary of an employee

#include < stdio.h >

int main()
{
    float bs, gs, da, hra;

    printf("Enter basic salary\n");
    scanf("%f", &bs);

    if( bs < 5000 )
    {
        hra = bs * 10 / 100;
        da  = bs * 90 / 100;
    }
    else
    {
        hra = 600;
        da  = bs * 95 / 100;
    }

    gs = bs + da + hra;

    printf("Gross Salary is Rs %f\n", gs);

    return 0;
}

Output 1:
Enter basic salary
5500
Gross Salary is Rs 11325.000000

Output 2:
Enter basic salary
4000
Gross Salary is Rs 8000.000000

Formula to calculate Gross Salary is:
Gross Salary = Basic Salary + Dearness allowance + House Rent Allowance.

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