Calculating Your 5 subjects Percentage in C language

 C language


Calculating Your 5 subjects Percentage in C language code here



#include <stdio.h>


int main() {

    // Variables to store marks

    float subject1, subject2, subject3, subject4, subject5;


    // Input marks for each subject

    printf("Enter marks for Subject 1: ");

    scanf("%f", &subject1);


    printf("Enter marks for Subject 2: ");

    scanf("%f", &subject2);


    printf("Enter marks for Subject 3: ");

    scanf("%f", &subject3);


    printf("Enter marks for Subject 4: ");

    scanf("%f", &subject4);


    printf("Enter marks for Subject 5: ");

    scanf("%f", &subject5);


    // Calculate percentage

    float totalMarks = subject1 + subject2 + subject3 + subject4 + subject5;

    float percentage = (totalMarks / 500) * 100;


    // Display the result

    printf("\nTotal Marks: %.2f\n", totalMarks);

    printf("Percentage: %.2f%%\n", percentage);


    return 0;

}



explain 

Step 1: Include Header File


#include <stdio.h>
This line includes the standard input-output header file, which is necessary for using functions like printf and scanf.

Step 2: Declare Variables


float subject1, subject2, subject3, subject4, subject5;
Here, we declare five float variables to store the marks for each subject.

Step 3: Input Marks


printf("Enter marks for Subject 1: ");
scanf("%f", &subject1);
// Repeat for subjects 2 to 5
These lines prompt the user to input marks for each subject, and scanf is used to store the input in the respective variables.

Step 4: Calculate Total Marks


float totalMarks = subject1 + subject2 + subject3 + subject4 + subject5;
This line calculates the total marks by adding the marks of all five subjects.

Step 5: Calculate Percentage


float percentage = (totalMarks / 500) * 100;
This line calculates the percentage based on the total marks obtained out of 500.

Step 6: Display Result


printf("\nTotal Marks: %.2f\n", totalMarks);
printf("Percentage: %.2f%%\n", percentage);
These lines display the total marks and percentage with two decimal places using printf.

Step 7: Return 0


return 0;
This line signifies the end of the program and returns 0 to the operating system, indicating successful execution.

This program takes input for marks in five subjects, calculates the total marks and percentage, and then displays the results.



thx for visit my blog ( student )





0 Comments