C Programming

Exercise C10: Answer
Write a C program that allows the user to enter in 5 grades, ie, marks between 1 - 100. The program must calculate the average mark, and state the number of marks less than 65.


        #include <stdio.h>

        main()
        {
                int grade;      /* to hold the entered grade */
                float average;  /* the average mark */
                int loop;       /* loop count */
                int sum;        /* running total of all entered grades */
                int valid_entry;        /* for validation of entered grade */
                int failures;   /* number of people with less than 65 */

                sum = 0;        /* initialise running total to 0 */
                failures = 0;

                for( loop = 0; loop < 5; loop = loop + 1 ) 
                {
                        valid_entry = 0;
                        while( valid_entry == 0 ) 
                        {
                                printf("Enter mark (1-100):");
                                scanf(" %d", &grade );
                                if ((grade > 1 ) {
                                       if( grade < 100 ) 
                                            valid_entry = 1;
                                }
                        }
                        if( grade < 65 )
                                failures++;
                        sum = sum + grade;
                }
                average = (float) sum / loop;
                printf("The average mark was %.2f\n", average );
                printf("The number less than 65 was %d\n", failures );
        }


©Copyright B Brown. 1984-1999. All rights reserved.