Write a program that calculates the average marks of three subjects of a student, entered by the user through keyboard and store them in three different float variables having names marks_subject1, marks_subject2, marks_subject3

The formula to calculate the average is:

average = (marks_subject1 + marks_subject2 + marks_subject3) / 3
Your program should also display the average calculated.
Now on the basis of the calculated average, it should be decided that the student is Excellent, Good or Fail using conditional statements.

1. Excellent if average is greater then or equal to 70.
2. Good if average is less then 70 and greater then or equal to 50.
3. Fail if average is less then 50.

Hint: if-else structure.

Dani AI

Generated

For : a compact, correct C++ example that reads three float marks (named marks_subject1, marks_subject2, marks_subject3), computes the average, prints it with two decimals, and classifies the result using a clear if/else chain. This also shows basic input validation and the common pitfalls hinted at (type and division), and responds to by providing a concrete, learnable example.

#include <iostream>
#include <iomanip>

int main() {
    float marks_subject1, marks_subject2, marks_subject3;
    std::cout << "Enter three marks: ";
    if (!(std::cin >> marks_subject1 >> marks_subject2 >> marks_subject3)) {
        std::cerr << "Invalid input\n";
        return 1;
    }

    float average = (marks_subject1 + marks_subject2 + marks_subject3) / 3.0f;
    std::cout << std::fixed << std::setprecision(2);
    std::cout << "Average: " << average << '\n';

    if (average >= 70.0f) {
        std::cout << "Excellent\n";
    } else if (average >= 50.0f) {
        std::cout << "Good\n";
    } else {
        std::cout << "Fail\n";
    }

    return 0;
}

Notes and common traps: use float or double to avoid integer division (divide by 3.0 or 3.0f); use else if so one category is chosen; always check cin for input failure; consider validating ranges (e.g., 0–100) if needed. Using std::fixed/std::setprecision makes the average easier to read. This pattern is stable and easy to expand (e.g., read marks in a loop or compute weighted averages).

Recommended Answers

All 6 Replies

what's your doubt?
Mention where you need help?

See This

See This

Why do you even give homework help at all ? isnt that what teacher are for ?

Why do you even give homework help at all ? isnt that what teacher are for ?

yes i take help for all

what's your doubt?
Mention where you need help?

i am new commer in c++. so that is my problem plz solve me

This problem is taken straight out of a text book and can be solved by any one who has heard of if-then-else constructs in ANY language... Go construct a "never heard of programming logic" forum somewhere...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.