The Program in C to accept and print a student’s result using a structure inputted by user is given below:
#include <stdio.h>
struct Student {
char name[100];
int roll_num;
float marks[5];
float total;
float percentage;
};
void inputStudent(struct Student *s) {
int i;
printf("Enter the student's name: ");
scanf("%s", s->name);
printf("Enter the student's roll number: ");
scanf("%d", &s->roll_num);
printf("Enter the student's marks in 5 subjects: ");
for (i = 0; i < 5; i++) {
scanf("%f", &s->marks[i]);
s->total += s->marks[i];
}
s->percentage = s->total / 5;
}
void printStudent(struct Student s) {
int i;
printf("Name: %s\n", s.name);
printf("Roll Number: %d\n", s.roll_num);
printf("Marks: ");
for (i = 0; i < 5; i++) {
printf("%.2f ", s.marks[i]);
}
printf("\n");
printf("Total: %.2f\n", s.total);
printf("Percentage: %.2f%%\n", s.percentage);
}
int main() {
struct Student s;
inputStudent(&s);
printStudent(s);
return 0;
}
Output:
Enter the student’s name: alex
Enter the student’s roll number: 17
Enter the student’s marks in 5 subjects: 90
91
96
92
90
Name: alex
Roll Number: 17
Marks: 90.00 91.00 96.00 92.00 90.00
Total: 459.00
Percentage: 91.80%
Pro-Tips💡
This program defines a struct “Student” with members: name, roll_num, marks, total and percentage.
The main function defines a struct of type “Student” and calls the function “inputStudent()” to input the student’s information and calculate the student’s total and percentage.
It then calls the function “printStudent()” to print the student’s information.
The function “inputStudent()” prompts the user to input the student’s name, roll number and marks in 5 subjects, it then calculates the total and percentage and stores them in the struct.
The function “printStudent()” prints the student’s name, roll number, marks, total and percentage.
Please note that the program uses scanf(“%s”,…) and scanf(“%d”,…) to read the input name
and roll number, and scanf(“%f”,…) to read the input marks, and it does not check for the validity of the input, you may want to add some validation on the input to make sure that the input is valid.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.