Write a program that accepts the marks of 5 subjects and finds the sum and percentage marks obtained by the student. content

Table of Contents

Table of Contents

Algorithem

here is the concept of the WAP that accepts the marks of 5 subjects and finds the sum and percentage marks obtained by the student:

  • The program will first ask the user to enter the marks of 5 subjects.
  • The marks will be stored in an array.
  • The sum of the marks will be calculated.
  • The percentage of marks will be calculated by dividing the sum of the marks by the maximum marks (which is usually 100).
  • The sum and percentage of marks will be printed.

Here is an example of the code that implements the concept:

Here’s an example program in both C and C++ that accepts the marks of 5 subjects, calculates the sum of marks, and calculates the percentage obtained by the student:

Python

def marks_calculator():
  """
  Calculates the sum and percentage marks obtained by the student.

  Returns:
    A tuple of the sum and percentage marks.
  """

  marks = []
  sum = 0
  for i in range(5):
    mark = int(input("Enter the marks of subject {}: ".format(i + 1)))
    marks.append(mark)
    sum += mark

  percentage = sum / 5 *
  return sum, percentage


if __name__ == "__main__":
  sum, percentage = marks_calculator()
  print("Sum of marks = {}".format(sum))
  print("Percentage = {}%".format(percentage))


Explain

This code first defines a function called marks_calculator(). This function takes no arguments and returns a tuple of the sum and percentage marks.

The function then defines an array called marks to store the marks of the 5 subjects. The sum of the marks is calculated by iterating through the array and adding each mark to the sum. The percentage of marks is calculated by dividing the sum of the marks by the maximum marks (which is usually 100).

The function then returns the sum and percentage marks.

The main function of the program calls the marks_calculator() function and prints the sum and percentage marks.

To run the program, save it as marks.py and run it using the following command:

python marks.py

C

#include <stdio.h>

float calculatePercentage(float totalMarks, float maxMarks) {
    return (totalMarks / maxMarks) * 100;
}

int main() {
    float subjectMarks[5];
    float sub[5];
    float totalMarks = 0.0;
    float maxMarks = 0.0;
     // Assuming each subject has a maximum     marks
  for (int j = 0; j < 5; j++) {
        printf("Enter max marks for subject %d: ", j + 1);
        scanf("%f", &sub[j]);
         maxMarks += sub[j];
    }

    printf("Total marks obtained: %.2f\n", maxMarks);
    // Accepting marks from the user
    for (int i = 0; i < 5; i++) {
        printf("Enter marks for subject %d: ", i + 1);
        scanf("%f", &subjectMarks[i]);
        totalMarks += subjectMarks[i];
    }

    // Calculating the percentage obtained
    float percentage = calculatePercentage(totalMarks, maxMarks);

    // Displaying the result
    printf("Total marks obtained: %.2f\n", totalMarks);
    printf("Percentage marks obtained: %.2f\n", percentage);

    return 0;
}

Output

C++

#include <iostream>

float calculatePercentage(float totalMarks, float maxMarks) {
    return (totalMarks / maxMarks) * 100;
}

int main() {
    float subjectMarks[5];
    float sub[5];
    float totalMarks = 0.0;
    float maxMarks = 0.0;  // Assuming each subject has a maximum of 100 marks

    for (int j = 0; j < 5; j++) {
        std::cout << "Enter marks for subject " << j + 1 << ": ";
        std::cin >> sub[j];
        maxMarks += sub[j];
    }

    // Accepting marks from the user
    for (int i = 0; i < 5; i++) {
        std::cout << "Enter marks for subject " << i + 1 << ": ";
        std::cin >> subjectMarks[i];
        totalMarks += subjectMarks[i];
    }

    // Calculating the percentage obtained
    float percentage = calculatePercentage(totalMarks, maxMarks);

    // Displaying the result
    std::cout << "Total  max marks : " << maxMarks << std::endl;
    std::cout << "Total marks obtained: " << totalMarks << std::endl;
    std::cout << "Percentage marks obtained: " << percentage << std::endl;

    return 0;
}
WAP that accepts the marks of 5 subjects and finds the sum and percentage marks obtained by the student.

Output

Explain

Both the C and C++ programs use an array subjectMarks to store the marks of the 5 subjects. The calculatePercentage() function takes the total marks and the maximum marks as inputs and calculates the percentage in the same way as the Python program.

The programs then prompt the user to enter the marks for each subject, calculate the total marks, and calculate the percentage obtained. Finally, they display the total marks obtained and the percentage marks obtained by the student.

70 / 100 SEO Score

2 thoughts on “Write a program that accepts the marks of 5 subjects and finds the sum and percentage marks obtained by the student. content

Leave a Reply

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