0% found this document useful (0 votes)
1 views

Struct

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Struct

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

/*Q1.

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

Enter the student's name: sita


Enter the roll number: 1
Enter the marks: 9

--- Student Details ---


Name: sita
Roll Number: 1
Marks: 9.00

Process returned 0 (0x0) execution time : 4.529 s


Press any key to continue.
/*Q2. Problem statement:
Create a structure to store information about a book (title, author, price) and write a program to display this
information.*/

#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

Enter the book title: Othello


Enter the author's name: William Shakespeare

Enter the price of the book: 109

--- Book Details ---


Title: Othello
Author: William Shakespeare
Price: 109.00
Process returned 0 (0x0) execution time : 10.595 s
Press any key to continue.

You might also like