C Exercises: Calculate the total, percentage and division to take marks of three subjects
Write a C program to read the roll no, name and marks of three subjects and calculate the total, percentage and division.
Sample Solution:
C Code:
#include <stdio.h> // Include the standard input/output header file.
#include <string.h> // Include the string handling functions.
void main()
{
int rl, phy, che, ca, total; // Declare variables for roll number, marks, total, etc.
float per; // Declare a variable to store percentage.
char nm[20], div[10]; // Declare arrays for name and division.
printf("Input the Roll Number of the student :"); // Prompt user for input.
scanf("%d", &rl); // Read and store roll number.
printf("Input the Name of the Student :"); // Prompt user for input.
scanf("%s", nm); // Read and store name.
printf("Input the marks of Physics, Chemistry and Computer Application : "); // Prompt user for input.
scanf("%d %d %d", &phy, &che, &ca); // Read and store marks for three subjects.
total = phy + che + ca; // Calculate total marks.
per = total / 3.0; // Calculate percentage.
if (per >= 60) // Check if percentage is greater than or equal to 60.
strcpy(div, "First"); // Assign division as "First" if condition is true.
else if (per < 60 && per >= 48) // Check if percentage is between 48 and 60.
strcpy(div, "Second"); // Assign division as "Second" if condition is true.
else if (per < 48 && per >= 36) // Check if percentage is between 36 and 48.
strcpy(div, "Pass"); // Assign division as "Pass" if condition is true.
else // If none of the above conditions are met.
strcpy(div, "Fail"); // Assign division as "Fail".
printf("\nRoll No : %d\nName of Student : %s\n", rl, nm); // Print roll number and name.
printf("Marks in Physics : %d\nMarks in Chemistry : %d\nMarks in Computer Application : %d\n", phy, che, ca); // Print marks in each subject.
printf("Total Marks = %d\nPercentage = %5.2f\nDivision = %s\n", total, per, div); // Print total marks, percentage, and division.
}
Sample Output:
Input the Roll Number of the student :784 Input the Name of the Student :James Input the marks of Physics, Chemistry and Computer Application : 70 80 90 Roll No : 784 Name of Student : James Marks in Physics : 70 Marks in Chemistry : 80 Marks in Computer Application : 90 Total Marks = 240 Percentage = 80.00 Division = First
Flowchart:
C Programming Code Editor:
Previous: Write a C program to calculate the root of a Quadratic Equation.
Next: Write a C program to read temperature in centigrade and display a suitable message according to temperature state below.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics