C, being a powerful and widely used programming language, allows developers to create efficient programs. In this tutorial, we’ll create a program that accepts marks for five subjects and calculates both the sum and the percentage marks obtained by a student.
Table of Contents
Problem Statement
WAP that accepts the marks of 5 subjects and finds the sum and percentage marks obtained by the student
Understanding the Solution
Our C program will take inputs for marks obtained in five subjects from the user. It will then calculate the total marks by adding up these subject marks and compute the percentage by dividing the total marks by the total possible marks (assuming each subject has a maximum of 100 marks).
Here’s the step-by-step guide to creating this program:
C Program For Sum and Average
#include <stdio.h>
void main() {
int marks[5], totalMarks = 0;
float percentage;
// Accepting marks for each subject
for (int i = 0; i < 5; i++) {
printf("Enter marks for subject %d: ", i + 1);
scanf("%d", &marks[i]);
totalMarks += marks[i];
}
// Calculating total and percentage
percentage = ((float)totalMarks / (5 * 100)) * 100;
// Displaying the results
printf("Total marks obtained: %d\n", totalMarks);
printf("Percentage marks obtained: %.2f%%\n", percentage);
}
Output
Explanation of C Program
- We start by declaring the necessary variables: an integer array of marks to store marks for each subject, an integer
totalMarks
to calculate the sum and a floatpercentage
to store the percentage. - Using a loop, the program prompts the user to input marks for each of the five subjects. These marks are stored in the
marks
array, and thetotalMarks
variable is incremented with each input. - The total marks are then divided by the total possible marks (5 subjects, each with a maximum of 100 marks) to calculate the percentage.
- Finally, the program displays the total marks obtained and the percentage of marks on the console.
This simple C program efficiently computes the total marks obtained and the percentage of marks based on the input provided by the user. It showcases the capability of C to perform calculations and display results effectively. You can further explore and modify this code to suit various requirements and expand its functionalities.
1 thought on “C Program to Calculate Sum and Percentage of Marks”