C Program to Calculate Employee Salary Learning C and looking for a practical application? This program demonstrates how to handle employee data, specifically calculating a monthly salary based on hours worked and hourly rate. You'll see how to accept an employee's ID, their total worked hours, and the amount received per hour, then display the calculated salary formatted to two decimal places. It's a great exercise for mastering basic input/output and arithmetic operations in C!
#include <stdio.h> // Essential for console input (scanf) and output (printf) int main() { char employeeID[11]; // Character array to store Employee ID (Max 10 chars + null terminator) int workedHours; // Total hours worked in the month float hourlyRate; // Amount received per hour (can be decimal) float salary; // Calculated monthly salary // Input Employee ID printf("Input the Employees ID(Max. 10 chars): "); scanf("%s", employeeID); // Reads string input for ID // Input total worked hours printf("Input the working hrs: "); scanf("%d", &workedHours); // Reads integer input for hours // Input salary amount per hour printf("Salary amount/hr: "); scanf("%f", &hourlyRate); // Reads float input for hourly rate // Calculate the salary salary = workedHours * hourlyRate; // Display the result in the specified format printf("Employees ID = %s\n", employeeID); printf("Salary = U$ %.2f\n", salary); // %.2f formats to two decimal places return 0; // Program finished successfully } Test Data & Expected Output: Test Data: Input the Employees ID(Max. 10 chars): 0342 Input the working hrs: 8 Salary amount/hr: 15000 Expected Output: Employees ID = 0342 Salary = U$ 120000.00
How This Program Works: Core Logic & Concepts
This C program clearly demonstrates how to handle different data types for practical calculations. It focuses on taking various inputs and presenting a formatted output.
Variables:
char employeeID[11]: Stores the employee's ID as a string (an array of characters). [11] allows for up to 10 characters plus a null terminator (\0), which marks the end of the string.
int workedHours: Stores the total hours as a whole number (int).
float hourlyRate: Stores the hourly pay as a floating-point number (float), allowing for decimal values.
float salary: Stores the calculated salary, also as a float, to handle decimal cents.
Input (scanf):
scanf("%s", employeeID): Reads the employee ID. %s is used for reading strings. Important: When reading a string with %s, you do not use the & (address-of) operator before the array name, as the array name itself already represents its starting address.
scanf("%d", &workedHours): Reads the integer hours. %d is for integers.
scanf("%f", &hourlyRate): Reads the floating-point hourly rate. %f is for floats.
Calculation:
salary = workedHours * hourlyRate;: A straightforward multiplication to get the total salary. C automatically handles the multiplication of int and float by promoting the int to a float before calculation.
Output (printf):
printf("Employees ID = %s\n", employeeID);: Prints the stored employeeID. %s is used to display strings.
printf("Salary = U$ %.2f\n", salary);: Prints the calculated salary.
The %.2f format specifier is crucial here:
%f: Indicates a floating-point number.
.2: Specifies that the number should be displayed with exactly two digits after the decimal point, which is ideal for currency.
U$: Simply prints the currency symbol as a literal string.
This program provides a solid foundation for handling different data types, user input, calculations, and formatted output—all essential skills in C programming.
Conclusion
You've successfully created a practical C program to calculate an employee's monthly salary! This exercise reinforces key C concepts like handling diverse input types (strings, integers, floats), performing basic arithmetic, and formatting output for readability. It's a fundamental skill with direct application in real-world data processing. Keep experimenting with different input values, or consider enhancing it to handle multiple employees or calculate taxes!