Date : 24/11/2023
Roll No. and Name: 23MEP050 Mazin Vora
Course code and Name: 1CS501 Computer Programming
Practical No.: 8
Aim: (a) : Create a structure which holds various attributes
(e.g., name, id, basic_salary, DA%, HRA%, total_salary
etc.) of an employee. Write a program which allows you to
scan these (except total_salary) attributes for 3 employees.
The program should support following operations:
i. Display (total salary of the selected employee)
ii. Max (find and display name of the employee with
maximum salary)
Methodology followed:
#include <stdio.h>
struct Employee
{
char name[100];
int id;
float basic_salary;
float DA;
float HRA;
float total_salary;
};
int main()
{
struct Employee employees[3];int
i;
for (i = 0; i < 3; i++) {
printf("Enter details for employee %d:\n", i+1);
printf("Name: ");
scanf("%s", employees[i].name);
printf("ID: ");
scanf("%d", &employees[i].id);
printf("Basic Salary: ");
scanf("%f", &employees[i].basic_salary);
printf("DA: ");
scanf("%f", &employees[i].DA);
printf("HRA: ");
scanf("%f", &employees[i].HRA); employees[i].total_salary
= employees[i].basic_salary
+(employees[i].basic_salary * (employees[i].DA/100))
+(employees[i].basic_salary * (employees[i].HRA/100));
}
int choice;
printf("Enter 1 to display total salary of selected employee\n");
printf("Enter 2 to find and display name of employee with maximum
salary\n");
scanf("%d", &choice);
if(choice == 1)
{
int id;
printf("Enter ID of employee: ");
scanf("%d", &id);
for (i = 0; i < 3; i++)
{
if (employees[i].id == id)
{
printf("Total salary of employee %s is %.2f\n",
employees[i].name, employees[i].total_salary);
break;
}
}
}
else if(choice == 2)
{
float max_salary =
employees[0].total_salary;int max_index =
0;
for (i = 1; i < 3; i++)
{
if (employees[i].total_salary > max_salary)
{
max_salary =
employees[i].total_salary;max_index
= i;
}
}
printf("Employee with maximum salary is
%s\n",employees[max_index].name);
}
else
{
printf("Invalid choice\n");
}
return 0;
}
Output:
(b) Create a structure for a complex number which has
a real part and an imaginary part. Add the 2 complex
numbers, store it in another complex number using a
user defined function and display the result as a
complex number.
Methodology followed:
#include <stdio.h> typedef struct complex
{
int real; int imag;
} complex;
complex addComplex(complex c1, complex c2)
{
complex result;
result.real = c1.real + c2.real; result.imag = c1.imag +
c2.imag; return result;
}
void displayComplex(complex c)
{
printf("Result: %d + %di\n", c.real, c.imag);
}
int main()
{
complex c1, c2, result;
printf("Enter real and imaginary parts of first number:
"); scanf("%d %d", &c1.real, &c1.imag);
printf("Enter real and imaginary parts of second
number: "); scanf("%d %d", &c2.real, &c2.imag);
result = addComplex(c1, c2); displayComplex(result);
return 0;
}
Output: