0% found this document useful (0 votes)
57 views

PROGRAM #13 Aim:Implementing Structure //program To Read Employee Data and Print It On Screen

The document describes a C program that implements structures to store employee data including name, age, and income. It defines a struct called employee with char, int, and unsigned int fields. It then declares a variable of this struct type, gets user input to populate the fields, and prints the employee details. A second program extends this to input and print details of multiple employees and find the one with the highest income. Further programs demonstrate writing user input to a file and copying the contents of one file to another.

Uploaded by

SunnyChoudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

PROGRAM #13 Aim:Implementing Structure //program To Read Employee Data and Print It On Screen

The document describes a C program that implements structures to store employee data including name, age, and income. It defines a struct called employee with char, int, and unsigned int fields. It then declares a variable of this struct type, gets user input to populate the fields, and prints the employee details. A second program extends this to input and print details of multiple employees and find the one with the highest income. Further programs demonstrate writing user input to a file and copying the contents of one file to another.

Uploaded by

SunnyChoudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

//PROGRAM #13

Aim :Implementing Structure


//Program to read employee data and print it on screen
#include<stdio.h>
struct employee
{
char name[20];
int age;
unsigned income;
};

int main()
{
struct employee e;
printf("Enter the name of employee: ");
gets(e.name);
printf("Enter age: ");
scanf("%d" , &e.age);
printf("Enter income: ");
scanf("%u" , &e.income);

printf("\nDetails of employee: \n");


printf("Employee name : %s" , e.name);
printf("\nAge : %d" , e.age);
printf("\nIncome : %u" , e.income);
return 0;}
OUTPUT:

LEARNING OUTPUT: Here we have learnt basics about structures in C.


//Program to read various employees data and print it and declares which has
highest income
#include<stdio.h>
#define MAX 10000
struct employee
{
char name[30];
int age;
int salary;
};
int main()
{
struct employee e[MAX];
int n, i, j;
int max = 0;
printf("Enter the number of employees: ");
scanf("%d" , &n);

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


{
printf("\n\nEnter details for employee #%d " , i+1);
fflush(stdin);
printf("\nName of employee: ");
gets(e[i].name);
printf("Enter the age: ");
scanf("%d" , &e[i].age);
printf("Enter the salary: ");
scanf("%d" , &e[i].salary);
}

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


{
printf("\n\n");
printf("Details of employee #%d" , i+1);
printf("\nName : %s" ,e[i].name);
printf("\nAge : %d", e[i].age);
printf("\nSalary : %d", e[i].salary);
printf("\n");
}

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


{
if(max<e[i].salary)
{
max=e[i].salary;
j=i;
}
}
printf("\n\nEmployee with maximum salary : %s and the salary is :
%d" , e[j].name , max);
return 0;
}

OUTPUT:

LEARNING OUTPUT: Here we have learnt to implement structures in C


programming.
//PROGRAM #14
//Implementing File Handling
/*Program to take string from user and save it on a file named emp.txt*/

#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
char name[20];
int age;
fp = fopen("emp.txt" , "w");
if(fp == NULL)
{
printf("File doesn't exists!!!!");
exit(0);
}

printf("Enter the name: ");


gets(name);
fprintf(fp , "NAME = %s" , name);
printf("Enter age: ");
scanf("%d" , &age);
fprintf(fp , "\nAGE = %d" , age);
printf("Successfully written to file...");

fclose(fp);
return 0; }
OUTPUT:

LEARNING OUTPUT: Here we have leant to copy the input to text file in C programmin
/*Program to read data from a file named emp.txt and copy it to another file
named emp2.txt*/

#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp1 , *fp2;
char ch;
fp1 = fopen("emp.txt" , "r");
if(fp1 == NULL);
fp2 = fopen("emp2.txt" , "w");
while (1)
{
ch = fgetc(fp1);
if (ch == EOF)
break;
else
fputc(ch, fp2);
}
printf("File copied Successfully!");
fclose(fp2);
fclose(fp1);
return 0;
}
OUTPUT:

LEARNING OUTPUT: Here we have leant to copy the data from one file to other.

You might also like