The following code sums up the total of all students name starting with ‘S’ and display it. Fill in the blanks with required statements. struct student {int exam no,lang,eng,phy,che,mat
Advertisements
Question
The following code sums up the total of all students name starting with ‘S’ and display it. Fill in the blanks with required statements.
struct student {int exam no,lang,eng,phy,che,mat,csc,total;char name[15];};
int main()
{
student s[20];
for(int i=0;i<20;i++)
{ ______ //accept student details }
for(int i=0;i<20;i++)
{
______ //check for name starts with letter “S”
______ // display the detail of the checked name
}
return 0;
}
Code Writing
Advertisements
Solution
#include <iostream>
#include <cstring> // Include for string handling
using namespace std;
struct student {
int exam_no, lang, eng, phy, che, mat, csc, total;
char name[15];
};
int main() {
student s[20];
// Accept student details
for (int i = 0; i < 20; i++) {
cout << "\nEnter student name: ";
cin >> s[i].name;
cout << "Enter student exam number: ";
cin >> s[i].exam_no;
cout << "Enter Language Mark: ";
cin >> s[i].lang;
cout << "Enter English Mark: ";
cin >> s[i].eng;
cout << "Enter Physics Mark: ";
cin >> s[i].phy;
cout << "Enter Chemistry Mark: ";
cin >> s[i].che;
cout << "Enter Maths Mark: ";
cin >> s[i].mat;
cout << "Enter Computer Science Mark: ";
cin >> s[i].csc;
s[i].total = s[i].lang + s[i].eng + s[i].phy + s[i].che + s[i].mat + s[i].csc;
}
cout << "\nDetails of students whose name starts with letter S:\n";
// Check for names starting with the letter "S" and display details
for (int i = 0; i < 20; i++) {
if (s[i].name[0] == 'S') {
cout << "\nName: " << s[i].name;
cout << "\nTotal Marks: " << s[i].total << endl;
}
}
return 0;
}
Output:
Details of students whose name starts with the letter S:
Name: Sam
Total Marks: 488
Name: Steve
Total Marks: 506
shaalaa.com
Is there an error in this question or solution?
