About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Wednesday, September 4, 2013

C Program : Print Details of 50 students using Structure

Problem Statement :

The annual examination is conducted for 50 students for three subjects. Write a program to read the data and determine the following:
  • (a) Total marks obtained by each student.
  • (b) The highest marks in each subject and the Roll No. of the student who secured it.
  • (c) The student who obtained the highest total marks.


#include<stdio.h>
#define S 50

struct student
  {
  char name[30];
  int rollno;
  int sub[3];
  };

void main()
{
int i,j,max,count,total,n,a[S],ni;
struct student st[S];
clrscr();

printf("Enter how many students: ");
scanf("%d",&n);

/* for loop to read the names and roll numbers*/
for(i=0;i<n;i++)
    {
    printf("\nEnter name and roll number for student %d : ",i);
    scanf("%s",&st[i].name);
    scanf("%d",&st[i].rollno);
    }

/* for loop to read ith student's jth subject*/
for(i=0;i<n;i++)
    {
    for(j=0;j<=2;j++)
        {
        printf("nEnter marks of student %d for subject %d : ",i,j);
        scanf("%d",&st[i].sub[j]);
        }
    }

 /* (i) for loop to calculate total marks obtained by each student*/

for(i=0;i<n;i++)
{
 total=0;
    for(j=0;j<3;j++)
        {
        total=total+st[i].sub[j];
        }
    printf("\nTotal marks obtained by student %s are %dn",st[i].name,total);
    a[i]=total;
}

/* (ii) for loop to list out the student's roll numbers who 
have secured the highest marks in each subject */

/* roll number who secured the highest marks */

for(j=0;j<3;j++)
{
max=0;
    for(i=0;i<n;i++)
    {
    if(max<st[i].sub[j])
        {
        max = st[i].sub[j];
        ni = i;
        }
    }
printf("\nStudent %s got maximum marks = %d in Subject : %d" , st[ni].name, max, j);
}

max =0;

for(i=0;i<n;i++)
{
    if(max< a[i])
    {
    max =a[i];
    ni =i;
    }
}

printf("n%s obtained the total highest marks.",st[ni].name);
getch();
}

Output :

Enter how many students: 2

Enter name and roll number for student 0 : Pritesh 1

Enter name and roll number for student 1 : Suraj 2

Enter marks of student 0 for subject 0 : 90

Enter marks of student 0 for subject 1 : 89

Enter marks of student 0 for subject 2 : 78

Enter marks of student 1 for subject 0 : 67

Enter marks of student 1 for subject 1 : 88

Enter marks of student 1 for subject 2 : 99

Total marks obtained by student Pritesh are 257

Total marks obtained by student Suraj are 254

Student Pritesh got maximum marks = 90 in Subject : 0
Student Pritesh got maximum marks = 89 in Subject : 1
Student Suraj got maximum marks = 99 in Subject : 2
Pritesh obtained the total highest marks.

No comments: