Call us at 0700-922-6559 or Click here

Write a program to enter marks of 6 different subjects and find out the total mark (Using cin and cout statement)

#include <iostream>

using namespace std;

int main() {

 // Declare variables to store marks

 int sub1, sub2, sub3, sub4, sub5, sub6, total;

 // Prompt the user to enter marks for each subject

 cout << “Enter marks for 6 subjects (out of 100 each):\n”;

 // Get marks from the user

 cout << “Enter marks of Hindi: “;

 cin >> sub1;

 cout << “Enter marks of English: “;

 cin >> sub2;

 cout << “Enter marks of Mathematics: “;

 cin >> sub3;

 cout << “Enter marks of Science: “;

 cin >> sub4;

 cout << “Enter marks of Social Studies: “;

 cin >> sub5;

 cout << “Enter marks of Computer Science: “;

 cin >> sub6;

 // Calculate the total marks

 total = sub1 + sub2 + sub3 + sub4 + sub5 + sub6;

 // Display the total marks

 cout << “\nTotal marks obtained: ” << total << ” out of 600\n”;

 return 0;

}

Output:

Enter marks for 6 subjects (out of 100 each):

Enter marks of Hindi: 85

Enter marks of English: 92

Enter marks of Mathematics: 78

Enter marks of Science: 89

Enter marks of Social Studies: 91

Enter marks of Computer Science: 84

Total marks obtained: 529 out of 600

Here’s a step-by-step explanation of the code:

1. Header and Namespace:

  • #include <iostream>: This line includes the iostream header file, which provides input and output functionalities like cin and cout.
  • using namespace std;: This line brings the std namespace into scope, allowing you to use elements like cin and cout without the std:: prefix.

2. Main Function:

  • int main(): This line defines the main function, where program execution begins.

3. Variable Declaration:

  • int sub1, sub2, …, total;: These lines declare integer variables to store marks for each subject (sub1 to sub6) and the total marks.

4. User Input:

  • cout << “Enter marks for 6 subjects…\n”;: This line prompts the user to enter marks.
  • cin >> sub1;: This line takes input for the first subject’s marks and stores it in the variable sub1.
  • … (similar lines for other subjects): The code continues to prompt and take input for the remaining subjects.

5. Calculation:

  • total = sub1 + sub2 + … + sub6;: This line calculates the total marks by adding the marks of all subjects and storing the result in the total variable.

6. Output:

  • cout << “\nTotal marks obtained: ” << total << ” out of 600\n”;: This line displays the calculated total marks to the user.

7. Return Statement:return 0;: This line indicates successful program termination.

Leave a Reply

Your email address will not be published. Required fields are marked *