Design a structure in C to represent an employee's information, including their name, employee ID, department, and salary. Additionally, write a program that allows users to input employee data, display employee information, and update an employee's salary. write a code for demonstrating the implementation of the structure and program functionalities.input and output Enter employee details:Enter employee name: John DoeEnter employee ID: 101Enter department: SalesEnter salary: 50000Employee Information:Name: John Employee ID: 101 Department:Sales Salary: 50000.00Enter new salary: 55000Salary updated successfully.Updated Employee Information:Name: JohnEmployee ID: 101Department: SalesSalary: 55000.00
Question
Design a structure in C to represent an employee's information, including their name, employee ID, department, and salary. Additionally, write a program that allows users to input employee data, display employee information, and update an employee's salary. write a code for demonstrating the implementation of the structure and program functionalities.input and output Enter employee details:Enter employee name: John DoeEnter employee ID: 101Enter department: SalesEnter salary: 50000Employee Information:Name: John Employee ID: 101 Department:Sales Salary: 50000.00Enter new salary: 55000Salary updated successfully.Updated Employee Information:Name: JohnEmployee ID: 101Department: SalesSalary: 55000.00
Solution 1
Here is the C code to implement the structure and program functionalities:
#include <stdio.h>
#include <string.h>
// Employee structure
typedef struct {
char name[50];
int id;
char department[50];
float salary;
} Employee;
// Function to get employee details
void getEmployeeDetails(Employee* e) {
printf("Enter employee name: ");
fgets(e->name, sizeof(e->name), stdin);
printf("Enter employee ID: ");
scanf("%d", &e->id);
getchar(); // To consume newline character
printf("Enter department: ");
fgets(e->department, sizeof(e->department), stdin);
printf("Enter salary: ");
scanf("%f", &e->salary);
getchar(); // To consume newline character
}
// Function to display employee details
void displayEmployeeDetails(Employee* e) {
printf("Employee Information:\n");
printf("Name: %s", e->name);
printf("Employee ID: %d\n", e->id);
printf("Department: %s", e->department);
printf("Salary: %.2f\n", e->salary);
}
// Function to update employee salary
void updateSalary(Employee* e, float newSalary) {
e->salary = newSalary;
printf("Salary updated successfully.\n");
}
int main() {
Employee e;
getEmployeeDetails(&e);
displayEmployeeDetails(&e);
printf("Enter new salary: ");
float newSalary;
scanf("%f", &newSalary);
updateSalary(&e, newSalary);
displayEmployeeDetails(&e);
return 0;
}
This program first gets the employee details from the user, then displays the entered details. It then asks for a new salary, updates the salary, and displays the updated details.
Solution 2
Here is a simple C program that implements the structure and functionalities you described:
#include <stdio.h>
#include <string.h>
// Employee structure
typedef struct {
char name[50];
int id;
char department[50];
double salary;
} Employee;
// Function to get employee details
void getEmployeeDetails(Employee* e) {
printf("Enter employee name: ");
scanf("%s", e->name);
printf("Enter employee ID: ");
scanf("%d", &(e->id));
printf("Enter department: ");
scanf("%s", e->department);
printf("Enter salary: ");
scanf("%lf", &(e->salary));
}
// Function to display employee details
void displayEmployeeDetails(Employee* e) {
printf("Employee Information:\n");
printf("Name: %s\n", e->name);
printf("Employee ID: %d\n", e->id);
printf("Department: %s\n", e->department);
printf("Salary: %.2lf\n", e->salary);
}
// Function to update employee salary
void updateEmployeeSalary(Employee* e, double newSalary) {
e->salary = newSalary;
printf("Salary updated successfully.\n");
}
int main() {
Employee e;
getEmployeeDetails(&e);
displayEmployeeDetails(&e);
printf("Enter new salary: ");
double newSalary;
scanf("%lf", &newSalary);
updateEmployeeSalary(&e, newSalary);
displayEmployeeDetails(&e);
return 0;
}
This program first defines an Employee structure with the required fields. It then defines functions to get employee details from the user, display these details, and update the salary. The main function uses these to get details for an employee, display them, update the salary, and display the updated details.
Similar Questions
Write a C program to store the information of employees in a company using structures. Each employee should have the following details:Employee code (integer)Employee name (string)Salary (float)Department number (integer)The program should allow the user to input the number of employees (n) and then input the details of each employee. i.Display the employee name with the maximum salary.ii. Display the average salary of all employees in the company.Test Case:3 // number of employees101 //Enter employee code:John Doe // Enter employee name:50000.00 // Enter salary:1 // Enter department number:102Jane Smith60000.001103Suresh Raina55000.002Output:Jane Smith55000.00 // avg of all employees salary
Consider a scenario where you need to manage records of employees in a company. Create aprogram in C that utilizes structures to handle employee information. The program shouldperform the following tasks:a. Define a structure named Employee with the following attributes:Employee ID (integer)Employee Name (string)Department (string)Salary (float)b. Create an array of structures to store information for a maximum of 5 employees.c. Display the information of the employees.
Problem StatementIn a corporate office, HR wants to implement a program to input an employee's ID and salary. Your task is to assist him in using pointers to efficiently manage and print the employee details, displaying the ID and salary for accurate record-keeping.Input format :The first line of input consists of an integer, representing the employee ID.The second line consists of a float value, representing the salary of the employee.Output format :The first line of output prints "ID: " followed by the employee ID, as an integer.The second line prints "Salary: " followed by the salary of the employee, as a float value, rounded off to two decimal places.
Implement a C++ program to manage the salary structure for employees at a university, including Head of Department (HOD), Faculty, and Technical Assistants. Begin by defining a base class Employee with common attributes name and basicPay. Create derived classes, such as HOD, Faculty, and TechnicalAssistant, that are inherited from the employee. Each derived class should have its own method, calculateSalary(), to compute the total salary based on its specific attributes and the basic pay. The salary should also include a bonus based on the overall salary:If the overall salary is less than 50,000, the bonus is 5% of the overall salary.If the overall salary is between 50,000 and 100,000, the bonus is 7% of the overall salary.If the overall salary is more than 100,000, the bonus is 10% of the overall salary.HODs receive additional grade pay, dearnessAllowance, house RentalAllowance, and travelAllowance; faculty receive house RentalAllowance and travelAllowance; and Technical Assistants receive the basic pay and travelAllowance. In the main() function, instantiate objects of each employee type with sample data, calculate their total salary and bonus using the calculateSalary() method, and display them.
For the Employee Class Program in this tutorial, what if we need the capability to update the salary for employees? What information is needed to perform this task, and what code segment implements obtaining it in a method?a.)Employees file information and the new salarydef upd_salary(): employees = read_employees() new_sal = input("Enter the new salary of the employee: ")b.)The new salarydef upd_salary(employees): new_sal = input("Enter the new salary of the employee: ")c.)Employee iddef upd_salary(employees): empid = input("Enter the employee ID: ")d.)Employee id and the new salarydef upd_salary(employees): empid = input("Enter the employee ID: ") new_sal = input("Enter the new salary of the employee: ")
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.