Programming and Data Structures Lab Programs 1
1. Write a C Program using structures to read and display
the information about a student.
#include <stdio.h>
struct Student {
int r_no;
char name[20];
float cgpa;
};
int main() {
struct Student s;
printf("Enter roll number: ");
scanf("%d", &s.r_no);
printf("Enter name: ");
scanf("%s", [Link]);
printf("Enter CGPA: ");
scanf("%f", &[Link]);
printf("\nStudent Information:\n");
printf("Roll No: %d\n", s.r_no);
printf("Name: %s\n", [Link]);
printf("CGPA: %.2f\n", [Link]);
return 0;
}
Output:
2. Write a C Program to read, display, add and subtract two
complex numbers.
#include <stdio.h>
struct Complex {
float real;
float imag;
};
void display(struct Complex c) {
printf("%.2f + %.2fi\n", [Link], [Link]);
}
BY MR. K. DILEEP KUMAR | ASST. PROFESSOR | DEPT. OF CSE 1
Programming and Data Structures Lab Programs 2
int main() {
struct Complex c1, c2, sum, diff;
printf("Enter real and imaginary parts of first
complex number:\n");
scanf("%f %f", &[Link], &[Link]);
printf("Enter real and imaginary parts of second
complex number:\n");
scanf("%f %f", &[Link], &[Link]);
[Link] = [Link] + [Link];
[Link] = [Link] + [Link];
[Link] = [Link] - [Link];
[Link] = [Link] - [Link];
printf("First Complex Number: ");
display(c1);
printf("Second Complex Number: ");
display(c2);
printf("Sum: ");
display(sum);
printf("Difference: ");
display(diff);
return 0;
}
Output:
3. Write a C Program to read and display the information of
a student using nested structure.
#include <stdio.h>
struct Date {
int day;
int month;
int year;
};
struct Student {
BY MR. K. DILEEP KUMAR | ASST. PROFESSOR | DEPT. OF CSE 2
Programming and Data Structures Lab Programs 3
int r_no;
char name[20];
float cgpa;
struct Date dob; // Date of Birth
};
int main() {
struct Student s;
printf("Enter roll number: ");
scanf("%d", &s.r_no);
printf("Enter name: ");
scanf("%s", [Link]);
printf("Enter CGPA: ");
scanf("%f", &[Link]);
printf("Enter date of birth (day month year): ");
scanf("%d %d %d", &[Link], &[Link],
&[Link]);
printf("\nStudent Details:\n");
printf("Roll No: %d\n", s.r_no);
printf("Name: %s\n", [Link]);
printf("CGPA: %.2f\n", [Link]);
printf("Date of Birth: %02d/%02d/%d\n", [Link],
[Link], [Link]);
}
Output:
4. Write a C Program, using an array of pointers to a
structure, to read and display the data of students.
#include <stdio.h>
#include <stdlib.h>
struct Student {
BY MR. K. DILEEP KUMAR | ASST. PROFESSOR | DEPT. OF CSE 3
Programming and Data Structures Lab Programs 4
int r_no;
char name[20];
float cgpa;
};
int main() {
int n;
printf("Enter number of students: ");
scanf("%d", &n);
struct Student* students[n];
for (int i = 0; i < n; i++) {
students[i] = (struct
Student*)malloc(sizeof(struct Student));
printf("\nEnter details for student %d:\n",
i+1);
printf("Roll No: ");
scanf("%d", &students[i]->r_no);
printf("Name: ");
scanf("%s", students[i]->name);
printf("CGPA: ");
scanf("%f", &students[i]->cgpa);
}
printf("\nStudents Details:\n");
for (int i = 0; i < n; i++) {
printf("Student %d: Roll No=%d, Name=%s,
CGPA=%.2f\n",
i+1, students[i]->r_no, students[i]->name,
students[i]->cgpa);
free(students[i]);
}
}
BY MR. K. DILEEP KUMAR | ASST. PROFESSOR | DEPT. OF CSE 4
Programming and Data Structures Lab Programs 5
5. Write a C Program to demonstrate arrays of Union
variables..
#include <stdio.h>
union Value {
int r_no;
float cgpa;
};
int main() {
union Value v[2];
// Using union for roll number
v[0].r_no = 101;
printf("v[0] Roll No: %d\n", v[0].r_no);
// Using union for cgpa
v[1].cgpa = 9.5;
printf("v[1] CGPA: %.2f\n", v[1].cgpa);
// Note: Each union variable can hold only one
member value at a time
return 0;
}
BY MR. K. DILEEP KUMAR | ASST. PROFESSOR | DEPT. OF CSE 5