1.
Structure for Date
Problem: Define a structure to represent a date and display it.
#include <stdio.h>
struct Date {
int day;
int month;
int year;
};
int main() {
struct Date today;
[Link] = 3;
[Link] = 11;
[Link] = 2024;
printf("Today's date is: %02d/%02d/%d\n", [Link], [Link], [Link]);
return 0;
}
[Link] a Structure for a Student
Problem: Create a structure to store information about a student (name, age, and grade).
#include <stdio.h>
#include <string.h>
struct Student {
char name[50];
int age;
float grade;
};
int main() {
struct Student student1;
strcpy([Link], "Alice");
[Link] = 20;
[Link] = 88.5;
printf("Student Name: %s\n", [Link]);
printf("Age: %d\n", [Link]);
printf("Grade: %.2f\n", [Link]);
return 0;
}
[Link] of Structures
Problem: Create an array of structures to hold multiple students' information
#include <stdio.h>
#include <string.h>
struct Student {
char name[20];
int age;
float grade;
};
int main() {
struct Student students[3];
// Input data for three students
for (int i = 0; i < 3; i++) {
printf("Enter name, age, and grade for student %d: ", i + 1);
scanf("%s %d %f", students[i].name, &students[i].age, &students[i].grade);
}
// Display the information
printf("\nStudent Information:\n");
for (int i = 0; i < 3; i++) {
printf("Name: %s, Age: %d, Grade: %.2f\n", students[i].name, students[i].age,
students[i].grade);
return 0;
[Link] with Nested Structures
Problem: Define a structure for a book that contains a nested structure for the author.
#include <stdio.h>
#include <string.h>
struct Author {
char name[20];
char nationality[20];
};
struct Book {
char title[100];
struct Author author;
int year;
};
int main() {
struct Book book;
strcpy([Link], "The Great Gatsby");
strcpy([Link], "F. Scott Fitzgerald");
strcpy([Link], "American");
[Link] = 1925;
printf("Book Title: %s\n", [Link]);
printf("Author: %s\n", [Link]);
printf("Nationality: %s\n", [Link]);
printf("Year: %d\n", [Link]);
return 0;
}
[Link] Structures to Functions
Problem: Write a function to modify a student's grade.
#include <stdio.h>
#include <string.h>
struct Student {
char name[50];
int age;
float grade;
};
void updateGrade(struct Student *s, float newGrade) {
s->grade = newGrade; // Update the grade using pointer
}
int main() {
struct Student student;
strcpy([Link], "Bob");
[Link] = 21;
[Link] = 75.0;
printf("Before update: %s's Grade: %.2f\n", [Link], [Link]);
updateGrade(&student, 85.5);
printf("After update: %s's Grade: %.2f\n", [Link], [Link]);
return 0;
}