EXP9 Writeup-1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

EXPERIMENT NUMBER: 9

Date of Performance:

Date of Submission:

Aim: Program to demonstrate structures in C.


9a. Write a program to store and display name, roll number and marks in three subjects of n
students using structure.
THEORY
Structures (also called structs) are a way to group several related variables into one place.
Each variable in the structure is known as a member of the structure. The values of a
structure are stored in contiguous memory locations. It is a user-defined data type that can be
used to group items of possibly different types into a single type. The struct keyword is used
to define the structure in the C programming language. Unlike an array, a structure can
contain many different data types (int, float, char, etc.).

Syntax to define structure:


struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
};

To access the structure, we need to create a variable of it.


Use the struct keyword inside the main() method, followed by the name of the structure and
then the name of the structure variable:

Struct myStructure {
int myNum;
char myLetter;
};

int main() {
struct myStructure s1;
return 0;
}
Algorithm:
Start
Define a structure called student that contains:
● A character array name[20] to hold the student's name.
● An integer roll_no to hold the student's roll number.
● Three integers: physics, chem, and maths to hold the marks in respective subjects.
Declare an array st of type student to hold information for up to 100 students.
Declare an integer variable n to hold the number of students.
Print a message to the user asking for the number of students.
Read the value of n from the user.
For each student (i from 0 to n-1):
● Print a message asking for the student's name, roll number, and marks in physics,
chemistry, and maths.
● Read the student's name, roll_no, physics, chem, and maths using scanf.
Print a header for the student information table.
For each student (i from 0 to n-1):
● Print the student's name, roll_no, physics, chem, and maths.
End

Source Code:
#include<stdio.h>
#include<math.h>

struct student
{
char name[20];
int roll_no;
int physics,chem,maths;
};

void main()
{
struct student st[100];
int n,i,j;
printf("Enter the number of students:");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("Enter the student's name, roll number and marks in three subjects:");
scanf("%s %d %d %d %d" , st[i].name,&st[i].roll_no , &st[i].physics ,
&st[i].chem ,

&st[i].maths);
}
printf("Name\tRoll No\tPhysics\tChem\tMaths\n");
printf("----------------------------------------------\n");
for(i=0;i<=n-1;i++)
{
printf("%s\t%d\t%d\t%d\t%d\
n",st[i].name,st[i].roll_no,st[i].physics,st[i].chem,st[i].maths);
}

}
Output:

CONCLUSION:

Sign & Remark:

R1 R2 R3 R4 R5 Total Signature
(3 Marks) (3 Marks) (3 Marks) (3 Mark) (3 Mark) (15 Marks)

You might also like