CODING SOLUTIONS

Question:-

Write a program to input data of 'n' students in the class in following order:

Name
Roll No.
Marks in Physics
Marks in Chemistry
Marks in Maths
and then calculate the percentage accquired in PCM and create a Rank List based on students average. Print the Rank List in the following pattern:

1:- Name: xyz | Roll No.: 000 | Percentage: 99.99%

Remember to check if program is giving proper ERROR display even on erroneous situations.




Answer:-

"SOURCE CODE"
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
    int roll;
    char name[20];
    int marksp;
    int marksc;
    int marksm;
    float per;
}stud;
int main()
{
    int n,i,j;
    printf("Enter no of students: ");
    scanf("%d",&n);
    if(n==0)
        printf("\nSorry, No. students is 0.\n");
    else if(n<0)
    {
        printf("\nERROR\nInvalid number of students.\n");
        exit(1);
    }
    else
    {
    stud class[n];
    printf("\n");
    for(i=0; i<n; i++)
    {
        printf("Please, Enter Student %d details! \n",i+1);
        printf("\n");
        printf("Enter Name: ")scanf("%s"class[i].name);
        printf("Enter Roll No: ")scanf("%d"&class[i].roll);
        printf("Enter marks in Physics: ")scanf("%d"&class[i].marksp);
        printf("Enter marks in Chemistry: ")scanf("%d"&class[i].marksc);
        printf("Enter marks in Maths: ")scanf("%d"&class[i].marksm);
        class[i].per=(class[i].marksp + class[i].marksc + class[i].marksm ) /3.0;
        printf("\n");
    }
    stud temp;
    for(i=0; i<n-1; i++)
    {
        for(j=0; j<n-1; j++)
        {
            if(class[j].per < class[j+1].per)
            {
                temp=class[j];
                class[j]=class[j+1];
                class[j+1]=temp;
            }
        }
    }
    printf("Rank list: \n");
    printf("\n");
    for(i=0; i<n; i++)
    {
        printf("%d:-  Name: %s | Roll No: %d | Percentage: %.2f%% \n",
        i+1,class[i].name,class[i].roll,class[i].per);
    }
    printf("\n");
    }
    return 0;
}

"TEST RUN OUTPUT"

Enter no of students: 5

Please, Enter Student 1 details! 

Enter Name: AMIT
Enter Roll No: 421
Enter marks in Physics: 64
Enter marks in Chemistry: 75
Enter marks in Maths: 62

Please, Enter Student 2 details!

Enter Name: RAJU
Enter Roll No: 563
Enter marks in Physics: 14
Enter marks in Chemistry: 62
Enter marks in Maths: 78

Please, Enter Student 3 details!

Enter Name: ANIL
Enter Roll No: 460
Enter marks in Physics: 57
Enter marks in Chemistry: 94
Enter marks in Maths: 65

Please, Enter Student 4 details!

Enter Name: SUMIT
Enter Roll No: 63
Enter marks in Physics: 78
Enter marks in Chemistry: 61
Enter marks in Maths: 43

Please, Enter Student 5 details!

Enter Name: HARSH
Enter Roll No: 637
Enter marks in Physics: 46
Enter marks in Chemistry: 79
Enter marks in Maths: 64

Rank list:

1:-  Name: ANIL | Roll No: 460 | Percentage: 72.00%
2:-  Name: AMIT | Roll No: 421 | Percentage: 67.00%
3:-  Name: HARSH | Roll No: 637 | Percentage: 63.00%
4:-  Name: SUMIT | Roll No: 63 | Percentage: 60.67%
5:-  Name: RAJU | Roll No: 563 | Percentage: 51.33%

Comments