Object

Write a program that reads the name, roll number and marks of three subjects of four students and prints the total marks.

Algorithm

  1. Declare a structure with the tag students.
  2. Declare string name and integer variables rn, m1, m2 and m3 as the members of structure.
  3. Declare an integer variable i.
  4. Declare a variable s[4] of data type struct students.
  5. Repeat FOR i = 0 to 3 by 1
    • Read structure members s[i].name, s[i].rn, s[i].m1, s[i].m2 and s[i].m3.
    • [End of FOR loop.]
  6. Repeat FOR i = 0 to 3 by 1
    • Write structure members s[i].name, s[i].rn, s[i].m1, s[i].m2 and s[i].m3.
    • Write s[i].m1 + s[i].m2 + s[i].m3
    • [End of FOR loop.]
  7. Exit.

Flowchart

C++ Source Code

// program 99
#include<iostream>
#include <string>
using namespace std;

struct students
{
  string name;
  int rn, m1, m2, m3;
};

int main()
{
  int i;
  students s[4];
  for (i = 0; i < 4; i++)
  {
    cout << "\nEnter your name: ";
    getline(cin, s[i].name);
    
    cout << "Enter roll number: ";
    cin >> s[i].rn;
    
    cout << "Enter your marks in Maths, Computer Sc., and Physics: ";
    cin >> s[i].m1 >> s[i].m2 >> s[i].m3;
    cin.ignore(); // Ignore the newline character in the input buffer
  }
  
  system("cls");
  cout << "\n\tMARKS OF STUDENTS WITH THEIR TOTAL";
  for (i = 0; i < 4; i++)
  {
    cout << "\n\n\tName: " << s[i].name << "\t Roll No.: " << s[i].rn;
    cout << "\n\tMaths: " << s[i].m1 << " Computer Sc.: " << s[i].m2 << " Physics: " << s[i].m3;
    int totalMarks = s[i].m1 + s[i].m2 + s[i].m3;
    cout << "\n\tTotal marks obtained: " << totalMarks;
  }
  return 0;
}

C Source Code

/*program 99*/
#include<stdio.h>

int main()
{
  int i;
  struct students
  {
   char name[50];
   int rn, m1, m2, m3;
  };
  struct students s[4];
  for(i=0; i<4; i++)
     {
     printf("\nEnter your name: ");
     gets(s[i].name);
     printf("Enter roll number: ");
     scanf("%d", &s[i].rn);
     printf("Enter your marks in Maths, Computer Sc., and Physics: ");
     scanf("%d%d%d", &s[i].m1, &s[i].m2, &s[i].m3);
     while (getchar() != '\n'); // Clear input buffer
     }
  printf("\n\tMARKS OF STUDENTS WITH THEIR TOTAL");
  for(i=0; i<4; i++)
     {
     printf("\n\n\tName: %s\t Roll No.: %d", s[i].name, s[i].rn);
     printf("\n\tMaths: %d Computer Sc.: %d Physics: %d", s[i].m1,
     s[i].m2, s[i].m3);
     printf("\n\tTotal marks obtained: %d", s[i].m1+s[i].m2+s[i].m3);
     }
  return 0;
}

Output

Input given:
Enter your name: Amir Bhatti
Enter roll number: 13620
Enter your marks in Maths, Computer Sc., and Physics: 83 91 75

Enter your name: Ibrar Ahmed
Enter roll number: 13105
Enter your marks in Maths, Computer Sc., and Physics: 39 48 46

Enter your name: Adeel Anwar
Enter roll number: 13111
Enter your marks in Maths, Computer Sc., and Physics: 74 71 69

Enter your name: Salma Bashir
Enter roll number: 12337
Enter your marks in Maths, Computer Sc., and Physics: 83 92 84
Ouput

        MARKS OF STUDENTS WITH THEIR TOTAL

        Name: Amir Bhatti        Roll No.: 13620
        Maths: 83 Computer Sc.: 91 Physics: 75
        Total marks obtained: 249

        Name: Ibrar Ahmed        Roll No.: 13105
        Maths: 39 Computer Sc.: 48 Physics: 46
        Total marks obtained: 133

        Name: Adeel Anwar        Roll No.: 13111
        Maths: 74 Computer Sc.: 71 Physics: 69
        Total marks obtained: 214

        Name: Salma Bashir       Roll No.: 12337
        Maths: 83 Computer Sc.: 92 Physics: 84
        Total marks obtained: 259
Design a site like this with WordPress.com
Get started