pc014.c ​
Metadata ​
- Author — Amit Dutta (amitdutta4255@gmail.com)
- Last updated — 30 Mar 2026
- License — MIT
Problem Statement ​
Problem Statement
Write a c program that defines a structure Student with the following members: roll (int), name (string), and marks (float). Do the below: Create an array to store details for 3 students. Read the details for these 3 student from a file named students.txt. (Assume the file contains data in the format: Roll Name Marks). Implement a recursive function float calculateTotal(struct Student arr[], int n) to calculate the sum of marks of all students in the array. Display each student's details and the total marks calculated by the recursive function.
Source Code ​
Printing the code
To print this file, open it on GitHub and click Raw before printing, or use the Download Raw button above and print directly from that page.
c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define FILENAME "students.txt"
typedef struct Student {
int roll;
char name[20];
float marks;
} Stu;
void printDetails(Stu *, int);
float calculateTotal(struct Student [], int);
int main() {
FILE *input = NULL;
Stu stu[3];
int i = 0;
input = fopen(FILENAME, "r");
if(input == NULL) {
printf("\nError opening file %s. Please try again.", FILENAME);
exit(1);
}
while(i < 3 && (fscanf(input, "%d %s %f", &stu[i].roll, &stu[i].name, &stu[i].marks) == 3)) i++;
printDetails(stu, 3);
printf("\n\nTotal Marks: %g", calculateTotal(stu, 3));
fclose(input);
return 0;
}
float calculateTotal(struct Student stu[], int n) {
if(n <= 0) {
return 0;
}
return stu[n - 1].marks + calculateTotal(stu, n-1);
}
void printDetails(Stu *stu, int n) {
int i;
printf("\n== Student Details ==");
for(i = 0; i < n; i++) {
printf("\nStudent Roll: %d"
"\nStudent Name: %s"
"\nStudent Marks: %g\n",
stu[i].roll, stu[i].name, stu[i].marks);
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50