Write a C program to input information about n students in a class given the following: Roll number, student name, total marks. The program should output the marks of a specified student given his/her roll number.

#include <stdio.h>
#include <ctype.h>

typedef struct Student {
    int roll;
    char name[20];
    float marks;
} Std;

int main(){
    int numOfStd, findRoll;
    
    printf("How many students? ");
    scanf("%d", &numOfStd);
    
    Std student[numOfStd];
    
    for(int i=0; i<numOfStd; i++){
        printf("Enter the details for no. %d student: \n", i+1);
        
        printf("\tRoll number? ");
        scanf("%d", &student[i].roll);        
        printf("\tName? ");
        scanf(" %[^\n]s", &student[i].name);        
        printf("\tTotal marks? ");
        scanf("%f", &student[i].marks);
    }
    printf("\n");
    for(int i=0; i<numOfStd; i++){
        printf("The details of no. %d student: \n", i+1);
        
        printf("\tRoll number: %d\n", student[i].roll);        
        printf("\tName: %s\n", student[i].name);        
        printf("\tTotal marks: %0.2f\n", student[i].marks);
    }
    
    do{
        printf("\nEnter students roll number to display marks: ");
        scanf("%d", &findRoll);
        int i;
        for(i=0; i<numOfStd; i++){
            if(student[i].roll == findRoll) {
                printf("\nMarks obtained by Roll No. %d and Name: %s is Total: %0.2f\n",
                student[i].roll, student[i].name, student[i].marks);
                
                break;
            }
        }
        if(i==numOfStd){
            printf("No such record found.\n");
        }
        printf("Press \"Y\" to continue. Any other to stop.");
        
        getchar();
        
    } while(121==tolower(getc(stdin)));
    
    
    
    
    return 0;
}

Comments