CHAPTER 6
STRUCTURE
Q1.WAP in c program to store information of a student using structure.
#include<stdio.h>#include<conio.h>struct student{ char name[100]; int rollno; float gpa;};int main(){ struct student s; printf("enter your name:"); gets(s.name); printf("enter the rollno :"); scanf("%d",&s.rollno); printf("enter your gpa:"); scanf("%f",&s.gpa);
printf("name=%s\n",s.name); printf("rollno=%d\n",s.rollno); printf("gpa=%.2f\n",s.gpa);
return 0;}
#include<stdio.h>
#include<conio.h>
struct student
{
char name[100];
int rollno;
float gpa;
};
int main()
{
struct student s;
printf("enter your name:");
gets(s.name);
printf("enter the rollno :");
scanf("%d",&s.rollno);
printf("enter your gpa:");
scanf("%f",&s.gpa);
printf("name=%s\n",s.name);
printf("rollno=%d\n",s.rollno);
printf("gpa=%.2f\n",s.gpa);
return 0;
}
Q2.WAP in c to print the n information of a students.
#include <stdio.h>
#include<stdlib.h>
struct student{
char subject[20];
int marks;
};
int main() {
struct student *ptr;
int n;
printf("enter the number of record:");
scanf("%d",&n);
ptr=(struct student*) malloc(n*sizeof(struct student));
for(int i=0;i<n;++i)
{
printf("enter the subject and marks:");
scanf("%s%d",(ptr+i)->subject,&(ptr+i)->marks);
}
printf("Displaying information\n");
for(int i=0;i<n;++i)
{
printf("%s\t%d\n",(ptr+i)->subject,(ptr+i)->marks);
}
return 0;
}
Q3.WAP in c by creating a structure having name, roll number, percentage as member. Complete the program to display the records of students having percentage greater than 60.
#include<stdio.h>
#include<conio.h>
struct student
{
char name[100];
int rollno;
float percentage;
};
int main()
{
struct student s[100];
int i=0,n;
printf("enter the number:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter your name:");
scanf("%s",s[i].name);
printf("enter roll no:");
scanf("%d",&s[i].rollno);
printf("enter your percentage:");
scanf("%f",&s[i].percentage);
}
printf("SN\tname\trollno\tpercentage\n");
for(i=0;i<n;i++)
{
if(s[i].percentage>60)
{
printf("%d\t%s\t%d\t%f\n",(i+1),s[i].name,s[i].rollno,s[i].percentage);
}
}
return 0;
}
Q4.WAP in c to create structure Time & Employee having hour, second as time member and name age as Employee to store information. (use nested structure)
#include<stdio.h>
#include<conio.h>
struct time
{
int hour;
int second;
};
struct employee
{
char name[100];
int age;
struct time t;
};
int main()
{
struct employee e;
printf("enter your name:");
scanf("%s",e.name);
printf("enter your age:");
scanf("%d",&e.age);
printf("enter your office time in hour and seconds:");
scanf("%d%d",&e.t.hour,&e.t.second);
printf("name=%s\n",e.name);
printf("age=%d\n",e.age);
printf("hour=%d & seconds=%d",e.t.hour,e.t.second);
return 0;
}
Q5.WAP in c to create a structure named book with name, author and publisher as its member. Write a program using this structure to read data of 50 books and display the name of those books published by "XYZ" publisher.
#include<stdio.h>
#include<string.h>
struct book
{
char name[30];
char author[30];
char publisher[30];
};
int main()
{
struct book p[100];
int i;
for(i=0;i<2;i++)
{
printf("enter the name of book:");
gets(p[i].name);
printf("enter the author of book:");
gets(p[i].author);
printf("enter the publisher of the book: ");
gets(p[i].publisher);
}
for(i=0;i<2;i++){
if(strcmp (p[i].publisher=="xyz")==0)
{
printf("name=%s\n",p[i].name);
printf("author=%s\n",p[i].author);
printf("publisher=%s\n",p[i].publisher);
printf("\n\n");
}
}
return 0;
}
