Program 1: Define a structure type struct personal that would contain person name, date of joining and salary using this structure to read this information of 5 people and print the same on screen.
Solution:
#include<stdio.h>
#include<conio.h>
struct personal
{
char
name[20];
char
date[20];
float
salary;
};
void main()
{
struct
personal person[5];
int
i;
clrscr();
for(i=0;i<1;i++)
{
printf("enter
details of person %d : \n", i+1);
printf("enter
name : ");
scanf("%s",
person[i].name);
gets(person[i].name);
printf("enter
joining date (dd-mm-year) : ");
scanf("%s",
person[i].date);
gets(person[i].date);
fflush(stdin);
printf("enter
salary : ");
scanf("%f",
&person[i].salary);
}
for(i=0;i<1;i++)
{
printf("\ndetails
of %d person : \n", i+1);
printf("name
= %s\n",person[i].name);
printf("joining
date = %s\n",person[i].date);
printf("salary
= %f\n",person[i].salary);
}
getch();
}
Output:
Program 2: Define structure data type called time_struct containing three member’s integer hour, integer minute and integer second. Develop a program that would assign values to the individual number and display the time in the following format: 16: 40:51
Solution:
#include<stdio.h>
#include<conio.h>
struct time
{
int
hours;
int
minutes;
int
seconds;
};
void main( )
{
struct
time t1, t2; //time structure
variable
int
h,m,s;
clrscr();
printf("enter
time 1: \n");
printf("enter
hours: ");
scanf("%d",
&t1.hours);
printf("enter
minutes: ");
scanf("%dâ",
&t1.minutes);
printf("enter
seconds: ");
scanf("%d", &t1.seconds);
printf ("The Time 1 is %d : %d : %d", t1.hours, t1.minutes, t1.seconds);
printf("\nenter
time 2: \n");
printf("enter
hours: ");
scanf("%d",
&t2.hours);
printf("enter
minutes: ");
scanf("%d",
&t2.minutes);
printf("enter
seconds: ");
scanf("%d",
&t2.seconds);
printf
("The Time 2 is %d : %d : %d", t2.hours, t2.minutes, t2.seconds);
h
= t1.hours + t2.hours;
m
= t1.minutes + t2.minutes;
s
= t1.seconds + t2.seconds;
printf
("\nSum of the two time's is %d:%d:%d", h, m, s);
getch();
}
Output:
Program 3: Define a structure called cricket that will describe the following information: Player name, Team name, Batting average. Using cricket, declare an array player with 50 elements and write a C program to read the information about all the 50 players and print team wise list containing names of players with their batting average.
Solution:
#include<stdio.h>
#include<string.h>
struct players
{
char player_name[25], team_name[25];
float batting_avg;
}ply[50];
int main()
{
int i,j,n;
struct players temp;
printf("enter number of players");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter details of %d players", i+1);
printf("\nEnter player name : ");
scanf("%s", ply[i].player_name);
printf("Enter team name : ");
scanf("%s", ply[i].team_name);
printf("Enter batting average : ");
scanf("%f", &ply[i].batting_avg);
}
printf("\n");
for(i=0;i<n;i++)
{
printf("\n%s\t%s\t%f\t", ply[i].player_name, ply[i].team_name, ply[i].batting_avg);
}
//team wise sorting
for(i=1;i<=n-1;i++)
{
for(j=1;j<=n-i;j++)
{
if(strcmp(ply[j-1].team_name, ply[j].team_name)>0)
{
temp=ply[j-1];
ply[j-1]=ply[j];
ply[j]=temp;
}
}
}
printf("\nDetails of players");
for(i=0;i<n;i++)
{
printf("\n%s\t\t%s\t\t%f\t\t", ply[i].player_name, ply[i].team_name, ply[i].batting_avg);
}
return 0;
}
Output:
Program 4: Design a structure student_record to contain name, branch and total marks obtained. Develop a program to read data for 10 students in a class and print them.
Solution:
#include<stdio.h>
#include<conio.h>
struct student
{
char name[50];
char branch[50];
int total;
};
void main( )
{
int i;
struct student s1[10];
clrscr();
for( i=0 ; i<3 ; i++)
{
printf("Enter Student Name : ");
scanf("%s",s1[i].name);
printf("Enter Student Branch : ");
scanf("%s",s1[i].branch);
printf("Enter Student Total Marks : ");
scanf("%d", &s1[i].total);
}
printf("Display Student Details\n");
for( i=0 ; i<3 ; i++)
{
printf("Student Name: %s\n", s1[i].name);
printf("Student Branch: %s\n", s1[i].branch);
printf("Student Total Marks: %d\n", s1[i].total);
}
getch();
}
Output:
Program 5: Write a program in C using structure to enter rollno, marks of the three subject for 3 students and find total obtained by each student.
Solution:
#include<conio.h>
struct marks
{
int rollno;
int sub1;
int sub2;
int sub3;
int total;
};
void main()
{
struct marks student[3];
int i;
clrscr();
//manual initialization : {{101,50,50,50,0}, {102,60,60,60,0}, {103,70,70,70,0}};
for( i=0 ; i<3 ; i++)
{
printf("enter student %d roll no : ", i+1);
scanf("%d", &student[i].rollno);
printf("enter student %d subject 1 marks : ", i+1);
scanf("%d", &student[i].sub1);
printf("enter student %d subject 2 marks : ", i+1);
scanf("%d", &student[i].sub2);
printf("enter student %d subject 3 marks : ", i+1);
scanf("%d", &student[i].sub3);
}
for(i=0;i<3;i++)
{
student[i].total = student[i].sub1 + student[i].sub2 +
}
printf("STUDENT \t TOTAL\n");
for(i=0; i<3; i++)
{
printf("Student[%d] \t %d\n", i+1, student[i].total);
}
getch();
}
Output: