You are on page 1of 6

Store Information and Display it Using Structure

#include <stdio.h>
struct student
{
char name[50];
int roll;
float marks;
} s;

int main()
{
printf("Enter information:\n");

printf("Enter name: ");


scanf("%s", s.name);

printf("Enter roll number: ");


scanf("%d", &s.roll);

printf("Enter marks: ");


scanf("%f", &s.marks);

printf("Displaying Information:\n");

printf("Name: ");
puts(s.name);

printf("Roll number: %d\n",s.roll);

printf("Marks: %.1f\n", s.marks);

return 0;
}
Output

Enter information:
Enter name: Jack
Enter roll number: 23
Enter marks: 34.5
Displaying Information:
Name: Jack
Roll number: 23
Marks: 34.5
C Program to Store Information of Students Using Structure
#include <stdio.h>
struct student
{
char name[50];
int roll;
float marks;
} s[10];

int main()
{
int i;

printf("Enter information of students:\n");

// storing information
for(i=0; i<10; ++i)
{
s[i].roll = i+1;

printf("\nFor roll number%d,\n",s[i].roll);

printf("Enter name: ");


scanf("%s",s[i].name);

printf("Enter marks: ");


scanf("%f",&s[i].marks);

printf("\n");
}

printf("Displaying Information:\n\n");
// displaying information
for(i=0; i<10; ++i)
{
printf("\nRoll number: %d\n",i+1);
printf("Name: ");
puts(s[i].name);
printf("Marks: %.1f",s[i].marks);
printf("\n");
}
return 0;
}
Output

Enter information of students:

For roll number1,


Enter name: Tom
Enter marks: 98

For roll number2,


Enter name: Jerry
Enter marks: 89
.
.
.
Displaying Information:

Roll number: 1
Name: Tom
Marks: 98
.
.
.
calculate mean of three subjects of each person

#include <stdio.h>
#define N 4

struct student{
char name[20];
int eng;
int math;
int phys;
double mean;
};

static struct student data[]={


{"Jack", 82, 72, 58, 0.0},
{"Young", 77, 82, 79, 0.0},
{"Steeve", 52, 62, 39, 0.0},
{"Mark", 61, 82, 88, 0.0}
};

int main(void)
{
int i, j;

// Calculation of mean of three subject


for(i=0; i<N; i++){
data[i].mean = (data[i].eng + data[i].math + data[i].phys)/3.0;
}

for(i=0; i<N; i++){


printf("%7s: Eng = %3d Math = %3d Phys = %3d:
Mean = %5.1f\n", data[i].name, data[i].eng, data[i].math,
data[i].phys, data[i].mean);
}

return (0);

}
(Execution result)
$ ./a.out
Jack: Eng = 82 Math = 72 Phys = 58: Mean = 70.7
Young: Eng = 77 Math = 82 Phys = 79: Mean = 79.3
Steeve: Eng = 52 Math = 62 Phys = 39: Mean = 51.0
Mark: Eng = 61 Math = 82 Phys = 88: Mean = 77.0
$

You might also like