0% found this document useful (0 votes)
6 views8 pages

IC Ass4

Nothing. Bmbbbbbb

Uploaded by

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

IC Ass4

Nothing. Bmbbbbbb

Uploaded by

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

ASSIGNMENT-4

VU22CSEN0100127

B.Venkata Satya

1. Write a C program that defines a structure called Student to store the details of a student including
name, roll number, and
marks in three subjects (Physics, Chemistry, and Mathematics). The program should prompt the user
to input the details of
three students, store them in an array of Student structures, and then print the details of all
students.

CODE

#include <stdio.h>

// Defining the structure for Student

struct Student { char name[50]; int

rollNumber; float physics, chemistry,

mathematics;

};

int main() { struct Student students[3]; // Array to store details

of 3 students

// Input details for 3 students

for (int i = 0; i < 3; i++) { printf("Enter details

for Student %d:\n", i + 1); printf("Name: ");

scanf(" %[^\n]", students[i].name);

printf("Roll Number: "); scanf("%d",

&students[i].rollNumber); printf("Marks in

Physics: "); scanf("%f", &students[i].physics);

printf("Marks in Chemistry: "); scanf("%f",

&students[i].chemistry); printf("Marks in

Mathematics: "); scanf("%f",

&students[i].mathematics);

}
// Print details of all students printf("\nDetails of

Students:\n"); for (int i = 0; i < 3; i++) { printf("Student

%d:\n", i + 1); printf("Name: %s\n", students[i].name);

printf("Roll Number: %d\n", students[i].rollNumber);

printf("Physics: %.2f\n", students[i].physics);

printf("Chemistry: %.2f\n", students[i].chemistry);

printf("Mathematics: %.2f\n", students[i].mathematics);

printf("\n");

return 0;

OUTPUT:
2. Write a C program that defines a structure called Book to store the details of books including title,
author, and price. The
program should then create an array of Book structures to store the details of three books. After
storing the details, the program should print the details of all books.

CODE:

#include <stdio.h>

// Defining the structure for Book

struct Book { char title[100];

char author[50];

float price;

};

int main() { struct Book books[3]; // Array to store

details of 3 books

// Input details for 3 books

for (int i = 0; i < 3; i++) { printf("Enter

details for Book %d:\n", i + 1);

printf("Title: "); scanf(" %[^\

n]", books[i].title);

printf("Author: "); scanf(" %[^\

n]", books[i].author);

printf("Price: ");

scanf("%f", &books[i].price);

// Print details of all books printf("\

nDetails of Books:\n");

for (int i = 0; i < 3; i++) {

printf("Book %d:\n", i + 1);

printf("Title: %s\n", books[i].title);


printf("Author: %s\n", books[i].author);

printf("Price: %.2f\n", books[i].price);

printf("\n");

return 0;

OUTPUT
3. Write a C program that defines a structure called Employee to store the details of an employee
including name, ID, and
salary. The program should then create a pointer to an Employee structure and allocate memory for
it dynamically using
malloc(). After allocating memory, prompt the user to input the details of an employee using the
pointer, and then print the details of the employee

CODE:

#include <stdio.h>

#include <stdlib.h> // For malloc and free


// Defining the structure for Employee

struct Employee { char name[50];

int id;

float salary;

};

int main() { struct Employee *emp; // Pointer to

Employee structure

// Dynamically allocate memory for one Employee structure

emp = (struct Employee *)malloc(sizeof(struct Employee));

if (emp == NULL) { printf("Memory

allocation failed!\n");

return 1;

// Input details of the employee

printf("Enter details of the employee:\n");

printf("Name: "); scanf(" %[^\n]", emp-

>name);

printf("ID: ");

scanf("%d", &emp->id);

printf("Salary: ");

scanf("%f", &emp->salary);

// Print the details of the employee printf("\

nDetails of the Employee:\n"); printf("Name: %s\n",

emp->name); printf("ID: %d\n", emp->id);

printf("Salary: %.2f\n", emp->salary);


// Free the allocated memory

free(emp);

return 0;

}
OUTPUT:

You might also like