Course Code and Name: 2CS101 Computer Programming Practical No: 8

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Roll1 No.

and name: 20bcl086- JAYDUTT PATEL


Course code and name: 2CS101 Computer programming
Practical no: 8

Practical No:- 8(a)


AIM:
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>
#include<math.h>
#include<string.h>
struct Employee{
char name[20];
int salary;};
void main(){
int n = 0;
printf("Please enter no of employees\n");
scanf("%d", &n);
struct Employee e[n];
printf("Name of employee and their salary\n");
for(int i = 0; i < n; i++){
scanf("%s %d", e[i].name, &e[i].salary);
}
int s = 0;
printf("Please enter position of employee\n");
2

scanf("%d", &s);
printf("The name of employee is %s and total salary is %d", e[s - 1].name, e[s - 1].salary);
}

Theoretical concepts used: structures and its functions

Input/Output :

Practical No. 8(b)


AIM:
Write a structure to accept item information such as name, quantity and unit
price. Structure should take information about 5 items. Create a user defined function
that calculates the cost of each item. Print details of each item such as:

Name Quantity Price Cost


Notebook 5 50.0 250.0
Pen Drive 2 500.0 1000.0
Pen 20 5.0 100.0

Methodology followed:
#include<stdio.h>
#include<math.h>
#include<string.h>
struct Items{
3

char name[20];
int quantity;
int prize;
int coast};
void function(struct Items e[]){
printf("Table is given below:\n\n");
for(int i = 0; i < 5; i ++){

printf("%s\t\t%d\t%d\t%d\n", e[i].name, e[i].quantity, e[i].prize,


e[i].prize*e[i].quantity);
}
}
void main(){
struct Items e[5];
printf("Name of item with quantity and prize of each\n");
for(int i = 0; i < 5; i++){
scanf("%s %d %d", e[i].name, &e[i].quantity, &e[i].prize);
}
function(e);

Theoretical concepts used: structure and functions

Input/Output :
4

Practical No. 8(c)


AIM:
Write 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 user
defined function and display the result as a complex number.

Methodology followed :
#include<stdio.h>
#include<math.h>
#include<string.h>
struct complex{
int real;
int imaginary} c1, c2;
void function(struct complex c1, struct complex c2){

printf("Addition of two complex number is %d + i(%d)", c1.real + c2.real, c1.imaginary +


c2.imaginary);
}
void main(){
printf("Enter complex number in format real imaginary\n");
scanf("%d %d", &c1.real, &c1.imaginary);
printf("Enter other complex number in format real imaginary\n");
5

scanf("%d %d", &c2.real, &c2.imaginary);


function(c1, c2);
}

Theoretical concepts used: structures and functions

Input/Output :

CONCLUSIONS:
In this whole practical I learn about how to use structure. In addition I learnt how to add
complex numbers with the help of structure and also learn how to make table

You might also like