1. WAP TO STORE INFORMATION OF A STUDENT AND DISPLAY THEM USING STRUCTURE .
#include <stdio.h>
struct Student {
char name[50];
int roll;
float marks;
} s;
int main() {
printf("Enter information:\n");
printf("Enter name: ");
gets(s.name);
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Enter marks: ");
scanf("%f", &s.marks);
printf("Displaying Information:\n");
printf("Name: ");
printf("%s", s.name);
printf("Roll number: %d\n", s.roll);
printf("Marks: %.1f\n", s.marks);
return 0;
}
2. WAP TO STORE INFORMATION OF 48 STUDENTS AND DISPLAY THEM USING STRUCTURE .
#include <stdio.h>
struct student {
char name[50];
int roll;
float marks;
} s[48];
int main() {
int i;
printf("Enter information of students:\n");
// storing information
for (i = 0; i < 48; ++i) {
printf("Enter name: ");
scanf("%s", s[i].name);
printf("Enter age: ");
scanf("%f", &s[i].age);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");
// displaying information
for (i = 0; i < 48; ++i) {
printf("Displaying information of student %d",i+1);
puts(s[i].name);
printf("Roll=%d Marks: %f", s[i].age,s[i].marks);
printf("\n");
}
return 0;
}
3. WAP to create a structure named date that has day,month and year as its members. Include this structure as a member in another structure named Employee which has name,id and salary as other member . Use this structure to read and display employees name ,id, date of birth and salary.
#include<stdio.h>
struct date
{
int day;
int month;
int year;
};
struct Employee
{
char name[10];
int id;
struct date dob;
float salary;
}e;
int main()
{
printf("enter the information of employee\n");
printf("Enter name\n");
scanf("%s",e.name);
printf("Enter id\n");
scanf("%d",&e.id);
printf("\n Day of birthday:\t");
scanf("%d",&e.dob.day);
printf("\n Month of birthday:\t");
scanf("%d",&e.dob.month);
printf("\n Year of birthday:\t");
scanf("%d",&e.dob.year);
printf("Enter salary\n");
scanf("%f",&e.salary);
printf("\n THE EMPLOYEE INFORMATION IS \n");
printf("NAME \t\t ID \t\t DAY\t\t MONTH\t\t YEAR\t\t SALARY\n");
printf("%s\t\t%d\t\t%d\t\t%d\t\t%d\t\t%.2f",e.name,e.id,e.dob.day,e.dob.month,e.dob.year,e.salary);
return 0;
}
4. WAP to create a structure named student that has name, roll, marks as its members. Assume appropriate types and size members . use this structure to read and display records of 5 students.Create two functions: one is to read the information of students and other function to display student information. Pass as array of structure to above functions as their argument.
#include<stdio.h>
struct Student
{
char name[20];
int age;
float marks;
}s1[5];
int i;
void display(struct Student st1[])
{
printf("Name \tRoll \t Marks\t\n");
for(i=0;i<5;i++)
{
printf("%s\t%d\t%f\n",st1[i].name,st1[i].age,st1[i].marks);
}
}
void read(struct Student st[])
{
for(i=0;i<5;i++)
{
printf("enter the information of student number %d\n",i+1);
printf("Name:\n");
scanf("%s",st[i].name);
//getchar();//
//gets(s1[i].name);
printf("Age\n");
scanf("%d",&st[i].age);
printf("Marks\n");
scanf("%f",&st[i].marks);
}
}
int main()
{
printf("Read the information\n");
read(s1);
printf("enter the information\n");
display(s1);
}
5. WAP to read the name , rollno and marks of 5 students using an array of structure . Display the name ,roll no of those students whose marks is greater than 50.
6. WAP to read the name , address and salary of 5 employees using an array of structures. Display the information of each employee in ascending order of their name.
7. Create a structure named book which has members name,pages and price. WAP to read name,no of pages of 5 books using array of structure . Display the name, no of pages and price of most expensive book.(having highest price)
#include<stdio.h>
int main()
{
struct Book
{
char name[10];
char author[20];
float price;
} b[5],temp;
int i,j;
printf("Enter the record of Books\n");
for (i = 0; i < 5;i++)
{
printf("Enter name of Book\n");
scanf("%s",b[i].name);
printf("Enter Author\n");
scanf("%s",b[i].author);
printf("Enter price\n");
scanf("%f", &b[i].price);
}
for (i = 0; i < 5;i++)
{
for (j = i + 1; j < 5;j++)
{
if(b[i].price>b[j].price)
temp = b[i];
b[i] = b[j];
b[j] = temp;
}
}
printf("The name of book with highest price is\n");
printf("\t%s\t%s\t%f", b[4].name, b[4].author, b[4].price);
}
8.WAP to read the name, address and price of N hotels from the keyboard using a structure variable and display the information of only those hotel whose address is ‘Kathmandu” district.
#include<stdio.h>
#include<string.h>
#define N 5
int main()
{
struct Hotel
{
char name[10];
char address[20];
float price;
} h[N];
int i;
printf("Enter the record of Hotels\n");
for (i = 0; i < N;i++)
{
printf("Enter name of Hotel\n");
scanf("%s",h[i].name);
printf("Enter Address\n");
scanf("%s",h[i].address);
printf("Enter price\n");
scanf("%f", &h[i].price);
}
printf("The name of hotels whose address is Kathmandu is\n");
for (i = 0; i < N;i++)
{
if(strcmp(h[i].address,"Kathmandu")==0)
printf("\t%s\t%s\t%f", h[i].name,h[i].address,h[i].price);
}
}
9. C Program to Add Two Complex Numbers by Passing Structure to a Function.
#include <stdio.h>
typedef struct complex {
float real;
float imag;
} complex;
complex add(complex n1, complex n2);
int main() {
complex n1, n2, result;
printf("For 1st complex number \n");
printf("Enter the real and imaginary parts: ");
scanf("%f %f", &n1.real, &n1.imag);
printf("\nFor 2nd complex number \n");
printf("Enter the real and imaginary parts: ");
scanf("%f %f", &n2.real, &n2.imag);
result = add(n1, n2);
printf("Sum = %.1f + %.1fi", result.real, result.imag);
return 0;
}
complex add(complex n1, complex n2) {
complex temp;
temp.real = n1.real + n2.real;
temp.imag = n1.imag + n2.imag;
return (temp);
}
10. C Program to Calculate Difference Between Two Time Periods using Structure.
#include <stdio.h>
struct TIME {
int seconds;
int minutes;
int hours;
};
void differenceBetweenTimePeriod(struct TIME t1,
struct TIME t2,
struct TIME *diff);
int main() {
struct TIME startTime, stopTime, diff;
printf("Enter the start time. \n");
printf("Enter hours, minutes and seconds: ");
scanf("%d %d %d", &startTime.hours,
&startTime.minutes,
&startTime.seconds);
printf("Enter the stop time. \n");
printf("Enter hours, minutes and seconds: ");
scanf("%d %d %d", &stopTime.hours,
&stopTime.minutes,
&stopTime.seconds);
// Difference between start and stop time
differenceBetweenTimePeriod(startTime, stopTime, &diff);
printf("\nTime Difference: %d:%d:%d - ", startTime.hours,
startTime.minutes,
startTime.seconds);
printf("%d:%d:%d ", stopTime.hours,
stopTime.minutes,
stopTime.seconds);
printf("= %d:%d:%d\n", diff.hours,
diff.minutes,
diff.seconds);
return 0;
}
// Computes difference between time periods
void differenceBetweenTimePeriod(struct TIME start,
struct TIME stop,
struct TIME *diff) {
while (stop.seconds > start.seconds) {
--start.minutes;
start.seconds += 60;
}
diff->seconds = start.seconds - stop.seconds;
while (stop.minutes > start.minutes) {
--start.hours;
start.minutes += 60;
}
diff->minutes = start.minutes - stop.minutes;
diff->hours = start.hours - stop.hours;
}
11. C Program to Add Two Distances (in inch-feet system) using Structures.
#include <stdio.h>
struct Distance {
int feet;
float inch;
} d1, d2, result;
int main() {
// take first distance input
printf("Enter 1st distance\n");
printf("Enter feet: ");
scanf("%d", &d1.feet);
printf("Enter inch: ");
scanf("%f", &d1.inch);
// take second distance input
printf("\nEnter 2nd distance\n");
printf("Enter feet: ");
scanf("%d", &d2.feet);
printf("Enter inch: ");
scanf("%f", &d2.inch);
// adding distances
result.feet = d1.feet + d2.feet;
result.inch = d1.inch + d2.inch;
// convert inches to feet if greater than 12
while (result.inch >= 12.0) {
result.inch = result.inch - 12.0;
++result.feet;
}
printf("\nSum of distances = %d\'-%.1f\"", result.feet, result.inch);
return 0;
}
12. Create an array of structure named employee with name and salary as structure member and the array of structure is passed to a function which sorts in ascending order on the basis of salary and display the sorted array from main using pointer notation.
#include<stdio.h>
#include<conio.h>
struct employee
{
char name[50];
float salary;
};
void sort(struct employee *, int);
int main()
{
struct employee e[50];
int n, i;
printf("Enter no of employee:");
scanf("%d", &n); for(i=0;i<n;i++)
{
fflush(stdin);
printf("Enter name of employee:");
gets((e+i)->name);
fflush(stdin);
printf("Enter salary:");
scanf("%f", &(e+i)->salary);
}
sort(e , n);
for(i=0; i<n; i++)
` {
printf("Name:%s\tSalary:%f\n",(e+i)->name,(e+i)->salary);
}
getch();
return 0;
}
void sort(struct employee *e, int n)
{
struct employee temp;
int i, j;
for(i=0; i<n; i++)
{
for(j=0; j<n-1; j++)
{
if((e+j)->salary>(e+j+1)->salary)
{
temp = *(e+j);
*(e+j) = *(e+j+1);
*(e+j+1) = temp;
}
}
}
}
13. Create a structure named Employee which has name,address and salary as member . WAP to read the name,address, and salary of 5 employees using an array of structure. Display name of employee, which has third highest salary.
14. Create a structure named student which has members: id, name and marks.Read id, name and marks of N students and print the records of students who have passed in distinction.
15. Create a structure named employee having empName, age and salary as its members . Read name,age and salary of a number of employees and write these data to a file named employee.txt . Read employee information again and again until the user wants to add more employees. Finally write a program to search information of a particular employee from the file.
#include<stdio.h>
#include<string.h>
int main()
{
struct Employee
{
char ename[10];
int age;
float salary;
}e;
FILE *fptr;
char y_n , name[10];
//int found=0;
fptr=fopen("employee.txt","w+");
if(fptr==NULL)
{
printf("file cannot be open");
}
do
{
printf("Employee Name:\t");
scanf("%s",e.ename);
printf("Employee Age:\t");
scanf("%d",&e.age);
printf("Employee Salary:\t");
scanf("%f",&e.salary);
fprintf(fptr,"%s\t%d\t%f\n",e.ename,e.age,e.salary);
getchar();
printf("Do you want to add another employee:? Press y or Y");
y_n=getchar();
}while(y_n=='y'|| y_n=='Y');
getchar();
printf("Enter name you want to search: ");
gets(name);
rewind(fptr);
while(fscanf(fptr,"%s%d%f",e.ename,&e.age,&e.salary)!=EOF)
{
if(strcmp(e.ename,name)==0)
{
//found=1;
printf("name \t Age\t Salary\n");
printf("%s\t%d\t%f",e.ename,e.age,e.salary);
}
}
}