PRACTICAL PROGRAMS:
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.
#include<stdio.h>
struct person
{
char name[20];
char date[20];
float salary;
};
void main()
{
struct person p[5];
int i;
for( i = 0 ; i < 5 ; i++)
{
printf("enter details of person %d : \n", i+1);
printf("enter name : ");
scanf("%s", p[i].name);
printf("enter joining date (dd-month-year) : ");
scanf("%s", p [i].date);
printf("enter salary : ");
scanf("%f", & p[i].salary);
}
for( i = 0 ; i < 5 ; i++)
{
printf("\ndetails of %d person : \n", i+1);
printf("name = %s\n", p[i].name);
printf("joining date = %s\n", p[i].date);
printf("salary = %f\n", p[i].salary);
}
getch();
}
Output:
enter details of 1 person :
enter name : alexa
enter joining date (dd-mm-yyyy) : 10-01-2022
enter salary : 50000
details of 1 person :
name = alexa
joining date = 10-01-2022
salary = 50000.000000
Program 2: 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.
#include<stdio.h>
void main()
{
struct cricket
{
char player_name[25], team_name[25];
float batting_avg;
} ply[5], temp;
int i, j;
for( i = 0 ; i < 50 ; 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);
}
for( i = 0 ; i < 50 ; i++)
{
for( j = i ; j < 50 ; j++)
{
if(strcmp(ply[i].team_name, strcmp(ply[j].team_name) > 0)
{
temp = ply[i];
ply[i] = ply[j];
ply[j] = temp;
}
}
}
printf("\nDetails of players");
for( i = 0 ; i < 50 ; i++)
{
printf("\n%s\t%s\t%f\t", ply[i].player_name, ply[i].team_name, ply[i].batting_avg);
}
getch();
}
Output:
Enter details of 1 players
Enter player name : rohit
Enter team name : india
Enter batting average : 44.32
Enter details of 2 players
Enter player name : virat
Enter team name : aus
Enter batting average : 49.56
Details of players
virat aus 49.560001
rohit india 44.320000
Program 3: 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.
#include<stdio.h>
struct student_record
{
char name[50];
char branch[50];
int total;
};
void main()
{
struct student_record s1[10];
int i;
for( i = 0 ; i < 10 ; 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 < 10 ; 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:
Enter Student Name : Harry
Enter Student Branch : IT
Enter Student Total Marks : 248
…
Display Student Details
Student Name: Harry
Student Branch: IT
Student Total Marks: 248
…
Program 4: Write a program in C using structure to enter rollno, marks of the three subject for 3 students and and find total obtained by each student.
#include<stdio.h>
struct marks
{
int rollno;
int sub1;
int sub2;
int sub3;
int total;
};
void main()
{
struct marks student[3];
int i;
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 + student[i].sub3;
}
printf("STUDENT \t TOTAL\n");
for( i = 0 ; i < 3 ; i++)
{
printf("Student[%d] \t %d\n", i+1, student[i].total);
}
getch();
}
Output:
enter student 1 roll no : 101
enter student 1 subject 1 marks : 50
enter student 1 subject 2 marks : 50
enter student 1 subject 3 marks : 50
enter student 2 roll no : 102
enter student 2 subject 1 marks : 60
enter student 2 subject 2 marks : 60
enter student 2 subject 3 marks : 60
enter student 3 roll no : 103
enter student 3 subject 1 marks : 70
enter student 3 subject 2 marks : 70
enter student 3 subject 3 marks : 70
STUDENT TOTAL
Student[1] 150
Student[2] 180
Student[3] 210
PRACTICE PROGRAMS:
Program 1: 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.
#include<stdio.h>
struct time_struct
{
int hours;
int minutes;
int seconds;
} ;
void main( )
{
struct time_struct t1, t2;
int h, m, s;
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:
enter time 1:
enter hours: 1
enter minutes: 20
enter seconds: 20
The Time 1 is 1 : 20 : 20
enter time 2:
enter hours: 2
enter minutes: 10
enter seconds: 10
The Time 2 is 2 : 10 : 10
Sum of the two time's is 3:30:30