Are you looking for a simple and easy way to write a C program that can calculate the salary of an employee? If yes, then you are in the right place. In this program, I will show you how to write a C program that accepts an employee's ID, total worked hours in a month and the amount he received per hour. Then, it will print the ID and salary (with two decimal places) of the employee for a particular month.
C Program
To write a C program for employee salary calculation, we need to follow these steps:
- Include the standard input/output header file stdio.h using the #include directive.
- Declare four variables: id, hours, rate, and salary of type int and float.
- Prompt the user to enter the employee's ID using the printf function and store it in the id variable using the scanf function.
- Prompt the user to enter the total worked hours in a month using the printf function and store it in the hours variable using the scanf function.
- Prompt the user to enter the amount received per hour using the printf function and store it in the rate variable using the scanf function.
- Calculate the salary by multiplying the hours and rate variables and store it in the salary variable.
- Print the employee's ID and salary with two decimal places using the printf function and the `%d` and %.2f format specifiers.
- Return 0 from the main function to indicate successful execution.
Here is the complete code of the C program for employee salary calculation:
#include <stdio.h>
int main()
{
// Declare variables
int id, hours;
float rate, salary;
// Input employee's ID, hours and rate
printf("Enter employee's ID: ");
scanf("%d", &id);
printf("Enter total worked hours in a month: ");
scanf("%d", &hours);
printf("Enter amount received per hour: ");
scanf("%f", &rate);
// Calculate salary
salary = hours * rate;
// Print ID and salary with two decimal places
printf("Employee's ID: %d\n", id);
printf("Salary: %.2f\n", salary);
return 0;
}
Output
Here is an example of running the C program for employee salary calculation:
Enter employee's ID: 101
Enter total worked hours in a month: 160
Enter amount received per hour: 25
Employee's ID: 101
Salary: 4000.00
Enter employee's ID: 102
Enter total worked hours in a month: 120
Enter amount received per hour: 30
Employee's ID: 102
Salary: 3600.00
Conclusion
In this program, we learned how to write a C program for employee salary calculation. We also learned how to compile and run the C program using a terminal or command prompt window. We hope you found this tutorial helpful and informative. If you have any questions or feedback, please feel free to leave a comment below. Happy coding!
We love your feedback and invite you to comment on our articles, exercises, examples, quizzes and others. Your feedback helps us make our content awesome and serve you better. Please leave a comment and tell us what you think. How did our content help you learn something new? Thank you for being a part of our community!