Define a structure of employee having data

Define a structure of employee having data
members name, address, age and salary. Take
data for n employee in an array dynamically
and find the average salary.

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

typedef struct{
    char name[30];
    int id;
    double salary;
} Employee;
  float avg;
  int sum,i;

int main()
{
    sum =avg= 0;
    //number of employees
    int n=2;

    //array to store structure values of all employees
    Employee employees[n];

    //Taking each employee detail as input
    printf("Enter %d Employee Details \n \n",n);
    for(int i=0; i<n; i++){


        printf("Employee %d:- \n",i+1);

        //Name
        printf("Name: ");
        scanf("%[^\n]s",employees[i].name);

        //ID
        printf("Id: ");
        scanf("%d",&employees[i].id);

        //Salary
        printf("Salary: ");
        scanf("%lf",&employees[i].salary);

        //to consume extra '\n' input
        char ch = getchar();

        sum=sum+employees[i].salary;

        printf("\n");
    }

    //Displaying Employee details
    printf("-------------- All Employees Details ---------------\n");
    for(int i=0; i<n; i++){
        printf("Name \t: ");
        printf("%s \n",employees[i].name);

        printf("Id \t: ");
        printf("%d \n",employees[i].id);

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

         avg=(float)sum/2;

   printf("Average of array values is %.2f", avg);
        printf("\n");
    }




    return 0;
}

Md Amir Hamzah

Md. Amir Hamzah is an overthinker by mind and a writer by heart! Along with pursuing his BSC in CSE, he is always on the verge to try something new with each sunrise in the tech and social world! Come join him in the journey and find answers to your queries! Right now I am working on The Tech Breach website www.thetechbreach.com/

Leave a Reply

Your email address will not be published. Required fields are marked *