Table of Contents
Practice programs on Structures
1. Program to Store Information of a Student Using Structure:
#include <stdio.h>
struct student
{
char name[50];
int roll;
float marks;
} s;
int main()
{
printf("Enter information:\n");
printf("Enter name: ");
fgets(s.name, sizeof(s.name), stdin);
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Enter marks: ");
scanf("%f", &s.marks);
printf("Displaying Information:\n");
printf("Name: ");
printf("%s", s.name);
printf("Roll number: %d\n", s.roll);
printf("Marks: %.1f\n", s.marks);
return 0;
}
************************************************************
2. Program to Add Two Distances (in inch-feet system) using Structures:
#include <stdio.h>
struct Distance
{
int feet;
float inch;
} d1, d2, result;
int main()
{
printf("Enter 1st distance\n");
printf("Enter feet: ");
scanf("%d", &d1.feet);
printf("Enter inch: ");
scanf("%f", &d1.inch);
printf("\nEnter 2nd distance\n");
printf("Enter feet: ");
scanf("%d", &d2.feet);
printf("Enter inch: ");
scanf("%f", &d2.inch);
result.feet = d1.feet + d2.feet;
result.inch = d1.inch + d2.inch;
// while inch is greater than 12, changing it to feet.
while (result.inch < 12.0)
{
result.inch = result.inch - 12.0;
++result.feet;
}
printf("\nSum of distances = %d\'-%.1f\"", result.feet, result.inch);
return 0;
}
*******************************************************************
3. Program to Add Two Complex Numbers by Passing Structure to a Function:
#include <stdio.h>
typedef struct complex
{
float real;
float imag;
} complex;
complex add(complex n1, complex n2);
int main()
{
complex n1, n2, result;
printf("For 1st complex number \n");
printf("Enter the real and imaginary parts: ");
scanf("%f %f", &n1.real, &n1.imag);
printf("\nFor 2nd complex number \n");
printf("Enter the real and imaginary parts: ");
scanf("%f %f", &n2.real, &n2.imag);
result = add(n1, n2);
printf("Sum = %.1f + %.1fi", result.real, result.imag);
return 0;
}
complex add(complex n1, complex n2)
{
complex temp;
temp.real = n1.real + n2.real;
temp.imag = n1.imag + n2.imag;
return (temp);
}
****************************************************************
4. Program to Calculate Difference Between Two Time Periods:
#include <stdio.h>
struct TIME {
int seconds;
int minutes;
int hours;
};
void differenceBetweenTimePeriod(struct TIME t1, struct TIME t2, struct TIME *diff);
int main() {
struct TIME startTime, stopTime, diff;
printf("Enter the start time. \n");
printf("Enter hours, minutes and seconds: ");
scanf("%d %d %d", &startTime.hours, &startTime.minutes, &startTime.seconds);
printf("Enter the stop time. \n");
printf("Enter hours, minutes and seconds: ");
scanf("%d %d %d", &stopTime.hours, &stopTime.minutes, &stopTime.seconds);
// Difference between start and stop time
differenceBetweenTimePeriod(startTime, stopTime, &diff);
printf("\nTime Difference: %d:%d:%d - ", startTime.hours, startTime.minutes, startTime.seconds);
printf("%d:%d:%d ", stopTime.hours, stopTime.minutes, stopTime.seconds);
printf("= %d:%d:%d\n", diff.hours, diff.minutes, diff.seconds);
return 0;
}
// Computes difference between time periods
void differenceBetweenTimePeriod(struct TIME start, struct TIME stop, struct TIME *diff) {
while (stop.seconds > start.seconds) {
--start.minutes;
start.seconds += 60;
}
diff->seconds = start.seconds - stop.seconds;
while (stop.minutes > start.minutes) {
--start.hours;
start.minutes += 60;
}
diff->minutes = start.minutes - stop.minutes;
diff->hours = start.hours - stop.hours;
}
***************************************************************
5. Program to Store Information of Students Using Structure:
#include <stdio.h>
struct student {
char firstName[50];
int roll;
float marks;
} s[10];
int main() {
int i;
printf("Enter information of students:\n");
// storing information
for (i = 0; i < 5; ++i)
{
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");
// displaying information
for (i = 0; i < 5; ++i)
{
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
return 0;
}
***********************************************************
6. Program to Store Data in Structures Dynamically:
#include <stdio.h>
#include <stdlib.h>
struct course {
int marks;
char subject[30];
};
int main() {
struct course *ptr;
int i, noOfRecords;
printf("Enter the number of records: ");
scanf("%d", &noOfRecords);
// Memory allocation for noOfRecords structures
ptr = (struct course *)malloc(noOfRecords * sizeof(struct course));
for (i = 0; i < noOfRecords; ++i)
{
printf("Enter the name of the subject and marks respectively:\n");
scanf("%s %d", &(ptr + i)->subject, &(ptr + i)->marks);
}
printf("Displaying Information:\n");
for (i = 0; i < noOfRecords; ++i)
printf("%s\t%d\n", (ptr + i)->subject, (ptr + i)->marks);
return 0;
}
**********************************************************
For more practice questions, go to Questionnare.






