Structures:
The
collection of dissimilar data types is known as Structures.
It
is used to store Student data, employee data, voter data...etc.
Structure Syntax: Ex:
The
variable of a structure is called as members of structure or elements of a
structure.
The
members of a structure can not access out side of the structure directly .it
will access only with help of struct type variable.
Syntax for Struct type variable :
Struct structname
structtypevar1, structtypevar1,...etc.
Ex: struct employee e1,e2...etc;
Syntax for calling members of a variable:
Structtypevariable.elementname;
Ex: e1.eno, e1.ename...etc;
/*Write a C program to demonstrate Structure */
|
/*Write a C program to demonstrate
Structure and accept the data at runtime
*/
|
#include<stdio.h>
#include<conio.h>
struct
student
{
int
sno;
char
sname[20];
int
m1,m2,m3,tot;
float
avg;
};
void
main()
{
struct student s={11,"sri",56,32,51};
clrscr();
s.tot=s.m1+s.m2+s.m3;
s.avg=s.tot/3;
printf("\nsno=%d",s.sno);
printf("\nsname=%s",s.sname);
printf("\nm1=%d",s.m1);
printf("\nm2=%d",s.m2);
printf("\nm3=%d",s.m3);
printf("\ntotal=%d",s.tot);
printf("\navg=%0.2f",s.avg);
getch();
}
|
#include<stdio.h>
#include<conio.h>
struct
student
{
int
sno;
char
sname[20];
int
m1,m2,m3,tot;
float
avg;
};
void
main()
{
struct student s;
clrscr();
printf("Enter
sno,sname,m1,m2,m3:");
scanf("%d%s%d%d%d",&s.sno,s.sname,&s.m1,&s.m2,&s.m3);
s.tot=s.m1+s.m2+s.m3;
s.avg=s.tot/3;
printf("\nsno=%d",s.sno);
printf("\nsname=%s",s.sname);
printf("\nm1=%d",s.m1);
printf("\nm2=%d",s.m2);
printf("\nm3=%d",s.m3);
printf("\ntotal=%d",s.tot);
printf("\navg=%0.2f",s.avg);
getch();
}
|
Result:
Output:
sno=11
sname=sri
m1=56
m2=32
m3=51
total=139
avg=46.00
|
Result:
Input:
Enter
sno,sname,m1,m2,m3:1
abcd
89
65
41
Output:
sno=1
sname=abcd
m1=89
m2=65
m3=41
total=195
avg=65.00
|
/*Write a C program to accept 2
students data at runtime using
Structure */
|
#include<stdio.h>
#include<conio.h>
struct
student
{
int
sno;
char
sname[20];
int
m1,m2,m3,tot;
float
avg;
};
void
main()
{
struct student s1,s2;
clrscr();
printf("\nEnter
student 1 details:");
printf("\nEnter
sno,sname,m1,m2,m3:");
scanf("%d%s%d%d%d",&s1.sno,s1.sname,&s1.m1,&s1.m2,&s1.m3);
s1.tot=s1.m1+s1.m2+s1.m3;
s1.avg=s1.tot/3;
printf("\nEnter
student 2 details:");
printf("\nEnter
sno,sname,m1,m2,m3:");
scanf("%d%s%d%d%d",&s2.sno,s2.sname,&s2.m1,&s2.m2,&s2.m3);
s2.tot=s2.m1+s2.m2+s2.m3;
s2.avg=s2.tot/3;
printf("\nStudent
1 details:");
printf("\nsno=%d",s1.sno);
printf("\nsname=%s",s1.sname);
printf("\nm1=%d",s1.m1);
printf("\nm2=%d",s1.m2);
printf("\nm3=%d",s1.m3);
printf("\ntotal=%d",s1.tot);
printf("\navg=%0.2f",s1.avg);
printf("\nStudent
2 details:");
printf("\nsno=%d",s2.sno);
printf("\nsname=%s",s2.sname);
printf("\nm1=%d",s2.m1);
printf("\nm2=%d",s2.m2);
printf("\nm3=%d",s2.m3);
printf("\ntotal=%d",s2.tot);
printf("\navg=%0.2f",s2.avg);
getch();
}
|
Result:
Input:
Enter
student 1 detailes:
Enter
sno,sname,m1,m2,m3:1
a
52
63
45
Enter
student 2 detailes:
Enter
sno,sname,m1,m2,m3:2
b
65
32
45
Output:
Student
1 details:
sno=1
sname=a
m1=52
m2=63
m3=45
total=160
avg=53.00
Student
2 details:
sno=2
sname=b
m1=65
m2=32
m3=45
total=142
avg=47.00
|
/*Write a C program to accept n students
data at runtime using Structure */
Or
/*Write a C program to demonstrate
Structure using arrays
*/
|
#include<stdio.h>
#include<conio.h>
struct
student
{
int
sno;
char
sname[20];
int
m1,m2,m3,tot;
float
avg;
};
void
main()
{
struct student s[20];
int
i,n;
clrscr();
printf("\nEnter
the no of student records:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter
student %d details:",i+1);
printf("\nEnter
sno,sname,m1,m2,m3:");
scanf("%d%s%d%d%d",&s[i].sno,s[i].sname,&s[i].m1,&s[i].m2,&s[i].m3);
s[i].tot=s[i].m1+s[i].m2+s[i].m3;
s[i].avg=s[i].tot/3;
}
for(i=0;i<n;i++)
{
printf("\nstudent
%d details:",i+1);
printf("\nsno=%d",s[i].sno);
printf("\nsname=%s",s[i].sname);
printf("\nm1=%d",s[i].m1);
printf("\nm2=%d",s[i].m2);
printf("\nm3=%d",s[i].m3);
printf("\ntotal=%d",s[i].tot);
printf("\navg=%0.2f",s[i].avg);
}getch();
}
|
Result:
Input:
Enter
the no of student records:1
Enter
student 1 details:
Enter
sno,sname,m1,m2,m3:1
xyz
56
85
96
Output:
student
1 details:
sno=1
sname=xyz
m1=56
m2=85
m3=96
total=237
avg=79.00
|
No comments:
Post a Comment