Using user define functions C++ Program to take a value from the to take a value from the user as input marks of five subjects Physics, Chemistry, Biology, Mathematics, and Computer. Calculate percentage and grade according to the following: Percentage >= 90% : Grade A, Percentage >= 80% : Grade B, Percentage >= 70% : Grade C, Percentage >= 60% : Grade D, Percentage >= 40% : Grade E, Percentage < 40% : Grade F, Write this program with the help of user define functions. Grade and Percentage Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
#include <iostream> using namespace std; // Function to calculate percentage using pass-by-value float calculatePercentageByValue(int physics, int chemistry, int biology, int mathematics, int computer) { // Calculate the total marks and percentage int totalMarks = physics + chemistry + biology + mathematics + computer; float percentage = (float)totalMarks / 5; return percentage; } // Function to determine grade using pass-by-value char determineGradeByValue(float percentage) { if (percentage >= 90) return 'A'; else if (percentage >= 80) return 'B'; else if (percentage >= 70) return 'C'; else if (percentage >= 60) return 'D'; else if (percentage >= 40) return 'E'; else return 'F'; } // Function to calculate percentage using pass-by-reference void calculatePercentageByReference(int physics, int chemistry, int biology, int mathematics, int computer, float &percentage) { // Calculate the total marks and percentage int totalMarks = physics + chemistry + biology + mathematics + computer; percentage = (float)totalMarks / 5; } // Function to determine grade using pass-by-reference void determineGradeByReference(float percentage, char &grade) { if (percentage >= 90) grade = 'A'; else if (percentage >= 80) grade = 'B'; else if (percentage >= 70) grade = 'C'; else if (percentage >= 60) grade = 'D'; else if (percentage >= 40) grade = 'E'; else grade = 'F'; } int main() { int physics, chemistry, biology, mathematics, computer; float percentageByVal, percentageByRef; char gradeByVal, gradeByRef; // Input marks for the five subjects cout << "Enter marks for Physics: "; cin >> physics; cout << "Enter marks for Chemistry: "; cin >> chemistry; cout << "Enter marks for Biology: "; cin >> biology; cout << "Enter marks for Mathematics: "; cin >> mathematics; cout << "Enter marks for Computer: "; cin >> computer; // Calculate percentage and grade using pass-by-value percentageByVal = calculatePercentageByValue(physics, chemistry, biology, mathematics, computer); gradeByVal = determineGradeByValue(percentageByVal); cout << "Using pass-by-value: Percentage = " << percentageByVal << "%, Grade = " << gradeByVal << endl; // Calculate percentage and grade using pass-by-reference calculatePercentageByReference(physics, chemistry, biology, mathematics, computer, percentageByRef); determineGradeByReference(percentageByRef, gradeByRef); cout << "Using pass-by-reference: Percentage = " << percentageByRef << "%, Grade = " << gradeByRef << endl; return 0; } |
Explanation:
- Pass-by-Value (
calculatePercentageByValue
anddetermineGradeByValue
):- Function
calculatePercentageByValue
:- Takes individual subject marks as parameters.
- Calculates the total marks and percentage.
- Returns the percentage.
- Function
determineGradeByValue
:- Takes the calculated percentage as a parameter.
- Determines the grade based on the percentage and returns it.
- Function
- Pass-by-Reference (
calculatePercentageByReference
anddetermineGradeByReference
):- Function
calculatePercentageByReference
:- Takes individual subject marks as parameters and a reference to a
float
for the percentage. - Calculates the total marks and percentage and updates the reference variable.
- Takes individual subject marks as parameters and a reference to a
- Function
determineGradeByReference
:- Takes the calculated percentage and a reference to a
char
for the grade. - Determines the grade based on the percentage and updates the reference variable.
- Takes the calculated percentage and a reference to a
- Function
- Main Function (
main
):- Prompts the user to input marks for the five subjects.
- Uses both pass-by-value and pass-by-reference methods to calculate the percentage and determine the grade.
- Displays the results.
Example Outputs:
Example 1:
Input:
- Physics:
85
- Chemistry:
90
- Biology:
80
- Mathematics:
88
- Computer:
92
Output:
1 2 3 4 5 6 7 |
Enter marks for Physics: 85 Enter marks for Chemistry: 90 Enter marks for Biology: 80 Enter marks for Mathematics: 88 Enter marks for Computer: 92 Using pass-by-value: Percentage = 87%, Grade = B Using pass-by-reference: Percentage = 87%, Grade = B |
Example 2:
Input:
- Physics:
50
- Chemistry:
60
- Biology:
55
- Mathematics:
70
- Computer:
65
Output:
1 2 3 4 5 6 7 |
Enter marks for Physics: 50 Enter marks for Chemistry: 60 Enter marks for Biology: 55 Enter marks for Mathematics: 70 Enter marks for Computer: 65 Using pass-by-value: Percentage = 60%, Grade = D Using pass-by-reference: Percentage = 60%, Grade = D |