Average Salary Calculator
Ready to dive into the world of programming? Join us on this exciting journey as we explore how to write a C program that calculates the average salary of 500 employees. In this beginner-friendly guide, we'll break down the steps and provide clear explanations, helping you grasp the fundamentals of coding while creating a practical program. Whether you're new to programming or looking to enhance your skills, this post is a perfect opportunity to learn and apply programming concepts in a real-world scenario. Let's get started and unlock the power of C programming together!
Learn how to write a C program to calculate the average salary of 500 employees using this informative guide.
#include <stdio.h>
int main() {
int numEmployees = 500;
float totalSalary = 0.0;
float averageSalary;
for (int i = 1; i <= numEmployees; i++) {
float salary;
printf("Enter salary of employee %d: ", i);
scanf("%f", &salary);
totalSalary += salary;
}
averageSalary = totalSalary / numEmployees;
printf("Average salary: %.2f\n", averageSalary);
return 0;
}
Test the Program
Enter salaries of 500 employees:
Explanation
-
Step 1:
The program starts by including the standard input/output library
stdio.h
. -
Step 2:
In the
main()
function, integer variablenumEmployees
and float variablestotalSalary
andaverageSalary
are declared. -
Step 3:
A
for
loop is used to input salaries of 500 employees and calculate the total salary. - Step 4: The average salary is calculated by dividing the total salary by the number of employees.
-
Step 5:
The program displays the calculated average salary using
printf()
.
If you have any doubts, Please let me know