Calculate total and percentage using JavaScript
In this article, we are going to know how to Calculate total and percentage using JavaScript where student names and marks of three subjects are input by the user. In this program, we are going to use prompt() to input data and calculate the total and percentage without using function and event.
Formula
total=subject1 + subject2 + subject3
Percentage = (Obtain marks/Total marks) X 100
Source Code:
Explanation:
In the above program, we use the prompt () function to input the students' names and marks for three subjects. We define the variable marks1, marks2, and marks3 respectively for three subject marks.
The parseFloat() function is used to convert the input data into a floating point number.
Now, it calculates the total marks by adding all three subjects and store in the variable total and calculates the percentage by dividing the total marks by 300 and multiplying by 100 and store in the variable per.
Finally, it displays the result by using the document.write() on the page.
Output:
Final words:
I hope you understood how to calculate total and percentage using JavaScript. In the next article, we are going to solve the same problem by using functions and buttons. If you have any suggestions, please feel me to write it the comment box.
No comments:
Post a Comment