C program for salary increment calculation using structure

Viewed 8757

I would like to write a code for a C program that will:

  1. Create a structure that will store the employee details.
  2. Insert 5 different records of employees into the database.
  3. Update the salary by adding a 10% increment if the years of service is 10 years and more. Otherwise, add a 7% increment.
  4. Display the data for all employees.

My coding has satisfied item 1,2 and 4. For item 3 i have no idea how to write the coding. Can anyone enlighten me?

Below the code

#include <stdio.h>
#include <stdlib.h>
 
typedef struct{ 
    char employee_name[30];
    int employee_number;
    int salary;
    int service_year 
} Employee;
 
int main()
{
    int i, n=5;
 
    Employee employees[n];
 
    //Taking each employee detail as input
 
    printf("Enter %d Employee Details \n \n",n);
    for(i=0; i<5; i++){
 
        printf("Employee %d:- \n",i+1);
        //Name
        printf("Name: ");
        scanf("%s",employees[i].employee_name);
        //ID
        printf("Id: ");
        scanf("%d",&employees[i].employee_number);
        //Salary
        printf("Salary: ");
        scanf("%d",&employees[i].salary);
        //Year of service_year
        printf("Year of Service: ");
        scanf("%d",&employees[i].service_year);
    }
 
    //Displaying Employee details
 
    printf("-------------- All Employees Details ---------------\n");
 
    for(i=0; i<n; i++){
 
        printf("Employee Name \t: ");
        printf("%s \n",employees[i].employee_name);
 
        printf("Employee Number \t: ");
        printf("%d \n",employees[i].employee_number);
 
        printf("Salary \t: ");
        printf("%d \n",employees[i].salary);
        
        printf("Year of Service \t: ");
        printf("%d \n",employees[i].service_year);
 
        printf("\n");
    }
 
    return 0;
}
3 Answers

Try to use floating point number for salary variable as after multiplication with 0.1 and 0.07, there will be information loss.
You also need to change the format specifier in printf and scanf to %f as the variable salary is of type float now.

Here is the code snippet to guide you.

Employee Struct

typedef struct
{
    
  char employee_name[30];
  int employee_number;
  float salary;
  int service_year
  
} Employee;

Calculate Increment

// Calculate the increment in salary
for(i=0; i<5; i++){
    if(employees[i].service_year >= 10)
       employees[i].salary+=(0.1)*employees[i].salary;
    else if(employees[i].service_year >=0 && employees[i].service_year < 10)
       employees[i].salary+=(0.07)*employees[i].salary;
}

To achieve your 3rd goal I only have created a method that receives the address of the employee struct, then I check the condition of his years of services, and finally, add the 10% or the 7%. In this approach i have directly incremented the salary when the data of the employee has been introduced, but feel free to call increment() whenever you want.

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
    
  char employee_name[30];
  int employee_number;
  float salary;
  int service_year
  
} Employee;
void increment (Employee * current);
int main ()
{
  int i, n = 5;

  Employee employees[n];

//Taking each employee detail as input

  printf ("Enter %d Employee Details \n \n", n);
  for (i = 0; i < 5; i++)
    {

      printf ("Employee %d:- \n", i + 1);
      //Name
      printf ("Name: ");
      scanf ("%s", employees[i].employee_name);
      //ID
      printf ("Id: ");
      scanf ("%d", &employees[i].employee_number);
      //Salary
      printf ("Salary: ");
      scanf ("%f", &employees[i].salary);
      //Year of service_year
      printf ("Year of Service: ");
      scanf ("%d", &employees[i].service_year);
      //printf ("Incrementing the salary according the years of service");
      increment (&employees[i]);

    }

//Displaying Employee details

  printf ("-------------- All Employees Details ---------------\n");

  for (i = 0; i < n; i++)
    {

      printf ("Employee Name \t: ");
      printf ("%s \n", employees[i].employee_name);

      printf ("Employee Number \t: ");
      printf ("%d \n", employees[i].employee_number);

      printf ("Salary \t: ");
      printf ("%f \n", employees[i].salary);

      printf ("Year of Service \t: ");
      printf ("%d \n", employees[i].service_year);

      printf ("\n");
    }

  return 0;

}

void increment (Employee * current)
{
  if (current->service_year >= 10)
    current->salary += current->salary * 0.1;
  else
    current->salary += current->salary * 0.07;
}

One output I have obtained using random employees. for my commodity and to check if this works, the salary of all of them is 100.

Enter 5 Employee Details 
 
Employee 1:- 
Name: Toni
Id: 23
Salary: 100
Year of Service: 10
Employee 2:- 
Name: Pedro
Id: 15
Salary: 100
Year of Service: 5
Employee 3:- 
Name: Juan
Id: 78
Salary: 100
Year of Service: 15
Employee 4:- 
Name: Ramon
Id: 55
Salary: 100
Year of Service: 1
Employee 5:- 
Name: Ester
Id: 44
Salary: 100
Year of Service: 55
-------------- All Employees Details ---------------
Employee Name   : Toni 
Employee Number     : 23 
Salary  : 110 
Year of Service     : 10 

Employee Name   : Pedro 
Employee Number     : 15 
Salary  : 107 
Year of Service     : 5 

Employee Name   : Juan 
Employee Number     : 78 
Salary  : 110 
Year of Service     : 15 

Employee Name   : Ramon 
Employee Number     : 55 
Salary  : 107 
Year of Service     : 1 

Employee Name   : Ester 
Employee Number     : 44 
Salary  : 110 
Year of Service     : 55 

For item 3 i have no idea how to write the coding.
Update the salary by adding a 10% increment if the years of service is 10 years and more. Otherwise, add a 7% increment.

I'd avoid floating point math for an integer problem with its subtle problems.

Add helper functions to calculate the salary increase.

A key part of programming is to divide the task into manageable helper functions. Note the two below each handle a separate aspect of the salary adjustment. Easy enough to adjust should next years adjusts taken on a more complex calculation and not mess up other code.

int increase_percent(int salary, int percent) {
  int half = percent >= 0 ? 100/2 : -100/2;
  return (salary * percent + half)/100; // add half for a rounded quotient
}

int increase_tenure(int salary, int years) {
  int percent = years >= 10 ? 10 : 7;
  return increase_percent(salary, percent);
}

// Usage
employees[i].salary += increase_tenure(employees[i].salary, employees[i].service_year);
Related