C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Calculate Gross Salary Program in C

Introduction

Gross salary is the total earnings of an employee before any deductions (like taxes, provident fund, etc.).
Typical components used to compute gross salary:

  • Basic Pay — base salary
  • Dearness Allowance (DA) — often a percentage of basic
  • House Rent Allowance (HRA) — often a percentage of basic
  • Transport Allowance (TA), Medical Allowance, and other allowances — either fixed or percentage-based

Gross Salary = Basic + DA + HRA + Other Allowances

The program below lets you either:

  1. Use default company percentages (DA = 10%, HRA = 20%, TA = 5%), or
  2. Enter custom percentage values for DA, HRA and a fixed amount for other allowances.

 

C Program: Gross Salary Calculator (with default & custom mode)

C

#include <stdio.h>

 

int main() {

    float basic, da, hra, ta, other_allowance;

    float da_percent, hra_percent, ta_percent;

    float gross;

    int mode;

 

    // Default company percentages (change if needed)

    const float DEF_DA_PCT = 10.0f;   // 10%

    const float DEF_HRA_PCT = 20.0f;  // 20%

    const float DEF_TA_PCT = 5.0f;    // 5%

 

    printf("Gross Salary Calculator\n");

    printf("-----------------------\n");

    printf("Enter Basic Pay: ");

    if (scanf("%f", &basic) != 1) {

        printf("Invalid input. Exiting.\n");

        return 1;

    }

 

    printf("\nChoose input mode:\n");

    printf("1. Use default percentages (DA=10%%, HRA=20%%, TA=5%%)\n");

    printf("2. Enter custom percentages/allowance\n");

    printf("Enter your choice (1 or 2): ");

    if (scanf("%d", &mode) != 1) {

        printf("Invalid input. Exiting.\n");

        return 1;

    }

 

    if (mode == 1) {

        da_percent = DEF_DA_PCT;

        hra_percent = DEF_HRA_PCT;

        ta_percent = DEF_TA_PCT;

        other_allowance = 0.0f; // no extra fixed allowance by default

    } else if (mode == 2) {

        printf("Enter DA percentage (e.g., 10 for 10%%): ");

        scanf("%f", &da_percent);

 

        printf("Enter HRA percentage (e.g., 20 for 20%%): ");

        scanf("%f", &hra_percent);

 

        printf("Enter TA percentage (e.g., 5 for 5%%). Enter 0 if none: ");

        scanf("%f", &ta_percent);

 

        printf("Enter any other fixed allowances (enter 0 if none): ");

        scanf("%f", &other_allowance);

    } else {

        printf("Invalid choice. Exiting.\n");

        return 1;

    }

 

    // Calculate individual components

    da = (da_percent / 100.0f) * basic;

    hra = (hra_percent / 100.0f) * basic;

    ta = (ta_percent / 100.0f) * basic;

 

    // Gross salary = basic + allowances

    gross = basic + da + hra + ta + other_allowance;

 

    // Display breakdown and final gross salary

    printf("\n--- Salary Breakdown ---\n");

    printf("Basic Pay              : %.2f\n", basic);

    printf("Dearness Allowance (%.2f%%): %.2f\n", da_percent, da);

    printf("House Rent Allowance (%.2f%%): %.2f\n", hra_percent, hra);

    printf("Transport Allowance (%.2f%%): %.2f\n", ta_percent, ta);

    if (other_allowance != 0.0f)

        printf("Other Fixed Allowances : %.2f\n", other_allowance);

    printf("-------------------------------\n");

    printf("Gross Salary           : %.2f\n", gross);

 

    return 0;

}

Output

 
OUTPUT 1 : Default mode
Gross Salary Calculator
-----------------------
Enter Basic Pay: 30000

Choose input mode:
1. Use default percentages (DA=10%, HRA=20%, TA=5%)
2. Enter custom percentages/allowance
Enter your choice (1 or 2): 1

--- Salary Breakdown ---
Basic Pay              : 30000.00
Dearness Allowance (10.00%): 3000.00
House Rent Allowance (20.00%): 6000.00
Transport Allowance (5.00%): 1500.00
-------------------------------
Gross Salary           : 40500.00



OUTPUT 2 : Custom mode
Gross Salary Calculator
-----------------------
Enter Basic Pay: 25000

Choose input mode:
1. Use default percentages (DA=10%, HRA=20%, TA=5%)
2. Enter custom percentages/allowance
Enter your choice (1 or 2): 2
Enter DA percentage (e.g., 10 for 10%): 12
Enter HRA percentage (e.g., 20 for 20%): 18
Enter TA percentage (e.g., 5 for 5%). Enter 0 if none: 3
Enter any other fixed allowances (enter 0 if none): 1500

--- Salary Breakdown ---
Basic Pay              : 25000.00
Dearness Allowance (12.00%): 3000.00
House Rent Allowance (18.00%): 4500.00
Transport Allowance (3.00%): 750.00
Other Fixed Allowances : 1500.00
-------------------------------
Gross Salary           : 34750.00