Write a program in C using structure to enter the roll_number, name and marks scored in english, computer, maths and nepali of 10 students. Also display them in proper format along with the total marks. [Note: the marks should be between 0 and 100]
#include <stdio.h>
// Define structure for student
struct Student {
int roll_number;
char name[50];
int english_marks;
int computer_marks;
int maths_marks;
int nepali_marks;
};
int main() {
// Define an array of structures to hold 10 students' information
struct Student students[10];
// Input information for each student
for (int i = 0; i < 10; i++) {
printf("Enter details for student %d:\n", i + 1);
printf("Roll Number: ");
scanf("%d", &students[i].roll_number);
printf("Name: ");
scanf("%s", students[i].name);
printf("English Marks: ");
scanf("%d", &students[i].english_marks);
printf("Computer Marks: ");
scanf("%d", &students[i].computer_marks);
printf("Maths Marks: ");
scanf("%d", &students[i].maths_marks);
printf("Nepali Marks: ");
scanf("%d", &students[i].nepali_marks);
// Validate marks range
if (students[i].english_marks < 0 || students[i].english_marks > 100 ||
students[i].computer_marks < 0 || students[i].computer_marks > 100 ||
students[i].maths_marks < 0 || students[i].maths_marks > 100 ||
students[i].nepali_marks < 0 || students[i].nepali_marks > 100) {
printf("Marks should be between 0 and 100. Please re-enter.\n");
i--; // Decrement i to re-enter details for this student
}
}
// Display information for each student along with total marks
printf("\n\n");
printf("Roll No\t Name\t English\t Computer\t Maths\t Nepali\t Total\n");
for (int i = 0; i < 10; i++) {
int total_marks = students[i].english_marks + students[i].computer_marks +
students[i].maths_marks + students[i].nepali_marks;
printf("%d\t %s\t %d\t\t %d\t\t %d\t %d\t %d\n", students[i].roll_number, students[i].name,
students[i].english_marks, students[i].computer_marks, students[i].maths_marks,
students[i].nepali_marks, total_marks);
}
return 0;
}
This program defines a structure Student
to hold the information of a student. Then it declares an array of structures to hold the information of 10 students. It takes input for each student's roll number, name, and marks in English, Computer, Maths, and Nepali subjects. It validates that marks should be between 0 and 100. Finally, it displays the entered information for each student along with the total marks.
0 Comments
I really appreciate for your words. Thank you very much for your comment.