Saturday, March 22, 2014

Aggregate and Percentage Marks - C Program

Problem Question


If the marks obtained by a student in five different subjects are input through the keyboard, find out the aggregate marks and percentage marks obtained by the student. Assume that the maximum marks that can be obtained by a student in each subject is 100.

Explanation of Problem


We wish to make a program over here which will let the user enter 5 numbers, which are the marks in 5 subjects out of 100. Out program should then calculate the aggregate and percentage marks, and display the result.

Code



#include <stdio.h>

/*Aggregate and Percentage Marks *
*@Language: ANSI C*
*@Compiler: GNU GCC*
*@IDE: Codelocks 12.11*
*@Author: Toxifier*
*@URL: http://letsplaycoding.blogspot.com/*
*@Date: *22-03-2014*/

int main()
{
    printf("\n\nEnter the marks in 5 subjects:\n");
    int marks[5], total = 0, i = 0;
    for(i = 0; i < 5; i++)
        {
                printf("\nEnter marks in Subject%d: ", i+1);
                scanf("%d", &marks[i]);
                total += marks[i];
        }
        printf("\nCalculating........\n");
        float percent = total / 5;
        printf("\nAggregate marks: %d\nPercentage makrs: %f\n\n", total, percent);
        system("pause");
        return 0;
}


Explanation of Code


#include <stdio.h> -> This is the step which occurs before compilation starts. The compiler calls the C Preprocessor to include the STDIO(Standard Input Output) header file into the program, thus letting the use of the standard input/output functions like printf() and scanf() which come from STDIO.H

int main() -> The entry point of the program where the execution starts. This functions has to named main. As per the ANSI specification, the return type has to be int. If you use the traditional C, you may use void as the return type. Since the return type is specified as int in my program, I have to use a return statement at the end of my code. So I use return 0 since zero returned from a function, by convention, implies a correct execution of the program. The return values are used to debug the program.

printf() -> This is a standard output function used to print something on the screen. We have to pass a string to this function which will be displayed on user's terminal.

scanf() -> This is the scanf() function which waits for the user to enter certain value using his/her keyboard. We store the user input at the location in memory which is pointed to by the variable whose address is passed to this function.

int marks[5], total = 0, i = 0; -> In this statement, I have declared an integer array called 'marks' of size 5. This means I have declared an array that can hold 5 integer values. The variable 'total' is used to store the aggregate marks. I initialized it to zero so that I can directly start adding the marks to it as they are entered by the user. The variable 'i' is used as the for loop counter variable.

    for(i = 0; i < 5; i++){
                printf("\nEnter marks in Subject%d: ", i+1);
                scanf("%d", &marks[i]);
                total += marks[i];}

-> This is the for loop, where I get the user inputs and keep adding them to get the aggregate marks, stored in the variable called 'total'.

float percent = total / 5; -> In this statement, I declare a variable of type float named 'percent' which stores the percentage marks. I simply divide the total by 5 to get the percentage.

system("pause") -> This statement is used to pause the program, until user presses a key. This function is not necessary in your program, I use it to see my outputs paused. If you use cmd to run your programs, you might not need this. If you use linux/unix you might not need this. Depending on your compiler, this function may or may not work. Moreover, removing this line of code from this program, doesn't affect the functionality of the program.

Output(s)



No comments:

Post a Comment

Need help?