Write a C program that defines the structure and store students' datas and print the datas after sorting according to roll

Here is the C program  that creates the structure student and store name , roll , address of the student.
Program asks user to input the number of students and its being sorted according to the roll number of the student

#include<stdio.h>
struct student {
    char name [50];
    int roll;
    char address[50];
    };
main()
{
int i,j,n,p,q,k;
struct student a[50];

printf("How many data you want to input?");
scanf("%d",&n);


for(i=0;i<n;i++)
{
printf("Enter your name ");
scanf("%s",&a[i].name);
printf("Enter your roll");
scanf("%d",&a[i].roll);
printf("Enter your address ");
scanf("%s",&a[i].address);
}
printf("\n");

for(j=0;j<n;j++)
{
printf("Name :- %s \n ",a[j].name);
printf("Roll Number :- %d\n",a[j].roll);
printf("Address :- %s \n ",a[j].address);
printf("\n");
}


struct student temp;
for(p=0;p<(n-1);p++)
{
    for(q=p+1;q<n;q++)
    {
        if(a[p].roll>a[q].roll)
        {
            temp = a[p];
       
            a[p] = a[q];
       
            a[q] = temp;

        }
    }
}

printf("After Sorting:-\n");
printf("********************************************************************************");

for(k=0;k<n;k++)
{
printf("Name :- %s \n ",a[k].name);
printf("Roll Number :- %d\n",a[k].roll);
printf("Address :- %s \n ",a[k].address);
printf("\n");
}


}

Comments