Struct
Struct
Problem statement:
Write a program to define a structure for a student and display its details (name, roll number, marks).*/
#include <stdio.h>
// Define the structure for a student
struct Student {
char name[50];
int rollNumber;
float marks;
};
int main() {
struct Student student; // Declare a variable of type Student
// Input details of the student
printf("Enter the student's name: ");
scanf("%s", student.name); // Read the name (single word input only)
printf("Enter the roll number: ");
scanf("%d", &student.rollNumber);
printf("Enter the marks: ");
scanf("%f", &student.marks);
// Display the student's details
printf("\n--- Student Details ---\n");
printf("Name: %s\n", student.name);
printf("Roll Number: %d\n", student.rollNumber);
printf("Marks: %.2f\n", student.marks);
return 0;
}
//OUTPUT
#include <stdio.h>
// Define the structure for a book
struct Book {
char title[100];
char author[100];
float price;
};
int main() {
struct Book book; // Declare a variable of type Book
// Input details of the book
printf("Enter the book title: ");
scanf("%99[^\n]", book.title); // Read title with spaces
getchar(); // Clear the newline character from the input buffer
printf("Enter the author's name: ");
scanf("%99[^\n]", book.author); // Read author name with spaces
getchar(); // Clear the newline character from the input buffer
printf("Enter the price of the book: ");
scanf("%f", &book.price);
// Display the book's details
printf("\n--- Book Details ---\n");
printf("Title: %s\n", book.title);
printf("Author: %s\n", book.author);
printf("Price: %.2f\n", book.price);
return 0;
}
//OUTPUT