Skip to main content

96 students record; pass marks

Short answer

Open "ioe.txt" in write mode; loop 96 times to read name, roll, marks and write each with fprintf. Reopen in read mode; fscanf each record; if marks >=40, print roll and name. Result: Outputs list of students scoring 40 or above.

Write a program in C, to read the following information for 96 students. Student Name, Student roll number, Marks obtained(in 100) Record all data in "ioe.txt" file, and program should print roll number and name of student who have obtained greater than or equal to 40 marks.

Logic.

  1. Open ioe.txt in write mode; read name, roll number and marks of 96 students in a loop and write each record with fprintf().
  2. Close the file, reopen it in read mode.
  3. Read the records back with fscanf() until it no longer returns 3, and print the roll number and name of those with marks >= 40.
#include <stdio.h>
#ifndef N
#define N 96
#endif

int main(void)
{
    FILE *fp;
    char name[30];
    int roll;
    float marks;

    fp = fopen("ioe.txt", "w");
    if (fp == NULL) {
        printf("Cannot create ioe.txt\n");
        return 1;
    }
    for (int i = 1; i <= N; i++) {
        printf("Student %d (name roll marks): ", i);
        scanf("%29s %d %f", name, &roll, &marks);
        fprintf(fp, "%s %d %.1f\n", name, roll, marks);
    }
    fclose(fp);

    fp = fopen("ioe.txt", "r");
    if (fp == NULL) {
        printf("Cannot open ioe.txt\n");
        return 1;
    }
    printf("\nStudents with marks >= 40\n%-6s %s\n", "Roll", "Name");
    while (fscanf(fp, "%29s %d %f", name, &roll, &marks) == 3)
        if (marks >= 40)
            printf("%-6d %s\n", roll, name);
    fclose(fp);
    return 0;
}

Sample run (N reduced to 3 with -DN=3 for the test; the real program uses 96)

Input (entered one after another): Ram 1 65 Sita 2 32 Hari 3 40

Students with marks >= 40
Roll   Name
1      Ram
3      Hari

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 10 questions →