C Program For Student Data Using Structures

#include<stdio.h>
struct student
{
    char name[20];
    int rno;
    int m1,m2,m3;
};
int main()
{
    struct student a[10],temp;
    int i,j,n;
    printf("\nenter N value\n");
    scanf("%d",&n);
    printf("\nenter student details\n");
    for(i=0;i<n;i++)
    {
        printf("\n%dth student details\n",i);
        printf("\nenter student name\n");
        fflush(stdin);
        scanf("%s",a[i].name);
        printf("\nenter roll number\n");
        scanf("%d",&a[i].rno);
        printf("\nenter marks of m1,m2, m3\n");
        scanf("%d%d%d",&a[i].m1,&a[i].m2,&a[i].m3);
    }
    printf("\nentered data is \n");
    for(i=0;i<n;i++)
    {
        printf("\nrollNo - %d\tstudent_name - %s\tm1 - %d\tm2 - %d\tm3 - %d\n",a[i].rno,a[i].name,a[i].m1,a[i].m2,a[i].m3);
    }
    for(i=0;i<n-1;i++)
    {
        for(j=1;j<(n-i);j++)
        {
            if((a[j-1].m1 + a[j-1].m2 + a[j-1].m3) > (a[j].m1 + a[j].m2 + a[j].m3))
            {
                temp = a[j];
                a[j] = a[j-1];
                a[j-1] = temp;
            }
        }
    }
   
    printf("\nsorted data is \n");
    for(i=0;i<n;i++)
    {
        printf("\nrollNo - %d\tstudent_name - %s\tm1 - %d\tm2 - %d\tm3 - %d\t%d\n",a[i].rno,a[i].name,a[i].m1,a[i].m2,a[i].m3,a[i].m1+a[i].m2+a[i].m3);
    }
    return(0);
}



Bhanu Namikaze

Bhanu Namikaze is an Ethical Hacker, Security Analyst, Blogger, Web Developer and a Mechanical Engineer. He Enjoys writing articles, Blogging, Debugging Errors and Capture the Flags. Enjoy Learning; There is Nothing Like Absolute Defeat - Try and try until you Succeed.

No comments:

Post a Comment