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

Structure

The document provides a series of C programs demonstrating the use of structures to manage and manipulate different types of data, including distances, student records, employee information, and book details. Each program includes user input, processing, and output, showcasing how to define and use structures effectively in C. Key examples include calculating distances, sorting student records, and finding the most and least expensive books.

Uploaded by

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

Structure

The document provides a series of C programs demonstrating the use of structures to manage and manipulate different types of data, including distances, student records, employee information, and book details. Each program includes user input, processing, and output, showcasing how to define and use structures effectively in C. Key examples include calculating distances, sorting student records, and finding the most and least expensive books.

Uploaded by

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

Structure

Introduction:
The collection of dissimilar data items treated as single unit is called structure. The struct
keyword is used to define structure.
Syntax:
Struct tag_name
{
data_ type member 1;
data_ type member 2;
................................
data_ type member n;
} var;
Memory size for var is 9 bytes.
1. Program to calculate the distance and distance is measured in terms of
feet, inch. (usetypedef)
#include<stdio.h>
#include<conio.h>
typedef struct
{
int feet;
int inch;
}distance;
int main()
{
distance d1,d2,d;
printf("enter the first distance in feeet inch format:");
scanf("%d%d",&d1.feet,&d1.inch);
printf("enter the second distance in feeet inch format:");
scanf("%d%d",&d2.feet,&d2.inch);
d.inch=(d1.inch+d2.inch)%12;
d.feet=d1.feet+d2.feet+(d1.inch+d2.inch)/12;
printf("sum of two distance=%dfeet%dinch",d.feet,d.inch);
getch();
}
OUTPUT
2. Program to input roll_no,fname and lname of 10 students prints the
same records on thescreen using structure.
#include<stdio.h>
struct student
{
int roll_no;
char fname[20];
char lname[20];
};
int main()
{
struct student s[10];
int i;
for(i=0;i<10;i++)
{
printf("Enter the roll no=");
scanf("%d",&s[i].roll_no);
printf("enter the fname=");
scanf("%s",s[i].fname);
printf("Enter the lname=");
scanf("%s",s[i].lname);
}
printf("\nROLL_NO \tFname \tlname");
for(i=0;i<10;i++)
printf("\n%d \t%s \t%s",s[i].roll_no,s[i].fname,s[i].lname);
}
OUTPUT
3. Program to considering user defined variable time with number: hh,
mm and ss. Calculate the sum of two different structure.
#include<stdio.h>
typedef struct
{
int hour;
int min;
int sec;
}
time;
int main()
{
time t1,t2,t;
printf("Enter the 1st time in hour minutes and second:");
scanf("%d%d%d",&t1.hour,&t1.min,&t1.sec);
printf("Enter the 2nd time in hour minutes and second:");
scanf("%d%d%d",&t2.hour,&t2.min,&t2.sec);
t.sec=(t1.sec+t2.sec)%60;
t.min=(t1.min+t2.min)%60+(t1.sec+t2.sec)/60;
t.hour=t1.hour+t2.hour+((t1.min+t2.min)/60+(t1.sec+t2.sec)/60);
printf("\nsum of two times =%d hour %d min %dsec",t.hour,t.min,t.sec);
}
OUTPUT
4. Program in C to input id,name, address and salary of
‘n’ employes and display the records of the employee
whose address is “bagmati”.
#include<stdio.h>
#include<string.h>
struct employee
{
int id;
char name[20];
char address[20];
}
s[2];
int main()
{
int i,bagmati;
for(i=0;i<2;i++)
{
printf("Enter the id:");
scanf("%d",&s[i].id);
printf("Enter the name:");
scanf("%s",s[i].name);
printf("enter the address:");
scanf("%s",s[i].address);
}
for(i=0;i<2;i++)
{
if(strcmp(s[i].address,"bagmati")==0)
{
printf("\n id=%d \n name=%s \n address=%s \n
",s[i].id,s[i].name,s[i].address);
}
}
}
OUTPUT
5. Program in C to input roll, name and address of
different students and display the same in ascending
order by their address using structure.
#include<stdio.h>
#include<string.h>
struct student
{
int roll_no;
char name[30];
char address[30];
}s[5];
int main()
{
struct student temp;
int i,j;
for(i=0;i<5;i++)
{
printf("\nEnter the roll_no:");
scanf("%d",&s[i].roll_no);
printf("\nEnter the name:");
scanf("%s",s[i].name);
printf("\nEnter the address:");
scanf("%s",s[i].address);
}
for(i=0;i<4;i++)
{
for(j=i+1;j<5;j++)
{
if(strcmp(s[i].address,s[j].address)>0)
{
temp=s[i];
s[i]=s[j];
temp=s[j];
}
}
}
printf("\nroll_no,name and address in ascending order\n");
printf("Roll_no=%d\nFirst Name=%s\nLast name=
%s",s[i].roll_no,s[i].name,s[i].address);
}
OUTPUT
6. Program to input id,name and salary of ‘n’ employes and prints the
record of the employees whose salary ranges from 15000 to 20000 using
structure.
#include<stdio.h>
int main()
{
int n,i;
printf("enter any number");
scanf("%d",&n);
struct employee
{
int id;
char name[20];
int salary;
}
s[n];
for(i=0;i<n;i++)
{
printf("Enter the id:");
scanf("%d",&s[i].id);
printf("Enter the name:");
scanf("%s",s[i].name);
printf("enter the salary:");
scanf("%d",&s[i].salary);
}
if(s[i].salary>15000&&s[i].salary<20000)
{
printf("\n id=%d \n name=%s \n salary=%d \n ",s[i].id,s[i].name,s[i].salary);
}
else
printf("sorry you have either low or high salary");
}
OUTPUT
7. Structure named Book to store book details like title, author, and
price. Write a C program to input details for five books, find the most
expensive and the lowest priced books, and display their information.
#include<stdio.h>
struct book
{
char title[50];
char author[50];
int price;
};
int main()
{
struct book b[5];
int i,exp=0,che=0;
for(i=0;i<3;i++)
{
printf("Enter Book Title:");
scanf("%s",b[i].title);
printf("Enter Book Author Name:");
scanf("%s",b[i].author);
printf("Enter Book Price:");
scanf("%d",&b[i].price);
}
for(i=0;i<3;i++)
{
if(b[i].price>b[exp].price)
{
exp=i;
}
if(b[i].price<b[che].price)
{
che=i;
}
}
printf("\nMost Expensive Book:\n");
printf("Title: %s\n", b[exp].title);
printf("Author: %s\n", b[exp].author);
printf("Price: %d\n", b[exp].price);
printf("\nLeast Expensive Book:\n");
printf("Title: %s\n", b[che].title);
printf("Author: %s\n", b[che].author);
printf("Price: %d\n", b[che].price);
}
OUTPUT
8. Program that takes students roll number, name, grade
and percentage and print the record of the student with
highest percentage.
#include<stdio.h>
struct student
{
int roll;
char name[50];
int grade;
float per;
}s[3];
int main()
{
struct student temp;
int i,j;
for(i=0;i<3;i++)
{
printf("Enter Roll number:");
scanf("%d",&s[i].roll);
printf("Enter Name:");
scanf("%s",s[i].name);
printf("Enter Grade:");
scanf("%d",&s[i].grade);
printf("Enter Percentage:");
scanf("%f",&s[i].per);
}
for(i=0;i<2;i++)
{
for(j=i+1;j<3;j++)
{
if(s[i].per<s[j].per)
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
printf("\nRoll.No\tName\tGrade\tPercentage");
for(i=0;i<3;i++)
printf("\n%d\t%s\t%d\t%f",s[i].roll,s[i].name,s[i].grade,s[i].per);
}
OUTPUT
9. Write a program to input roll, fname and lname and
total marks of 5 students and prints the same records in
descending order on the basis of total marks obtained
using structure.
#include<stdio.h>
struct student
{
int roll;
char fname[50],lname[50];
int mark;
}s[5];
int main()
{
struct student temp;
int i,j;
for(i=0;i<5;i++)
{
printf("Enter Roll number:");
scanf("%d",&s[i].roll);
printf("Enter First name:");
scanf("%s",s[i].fname);
printf("Enter Last name:");
scanf("%s",s[i].lname);
printf("Enter Total mark:");
scanf("%d",&s[i].mark);
}
for(i=0;i<4;i++)
{
for(j=i+1;j<5;j++)
{
if(s[i].mark<s[j].mark)
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
printf("\nRoll.No\tFirst name\tLast name\tTotal mark");
for(i=0;i<5;i++)
printf("\n%d\t%s\t\t%s\t\t%d",s[i].roll,s[i].fname,s[i].lname,s[i].mark);
}
OUTPUT

You might also like