Illustration of nested for loops

Program 23) A class of n students take an annual examination in m subjects. Given program read the marks obtained by each students in various subjects and to compute and print the total marks obtained by each of them.

The program uses two for loops, one for controlling the number of students and the other for controlling the number of subjects. Since both the number of students and the number of subjects are requested by the program, the program may be used for a class of any size and any number of subjects.
The outer loop includes three parts:
           (1) reading of roll numbers of students, one 
                after another;
           (2) inner loop, where the marks are read and 
                totalled for each student; and
           (3) printing of total marks and declaration
                of grades.

                                     PROGRAM

#define FIRST 360
#define SECOND 240
main ( )
 {
         int n, m, i, j, roll_numbers, marks, total ;
         printf ("Enter number of students and subjects\n") ;
         scanf ("%d %d", &n, &m) ;
         for (i = 1; i <= n; ++i)
          {
                printf ("Enter roll_number : ") ;
                scanf (%d', &roll_number) ;
                total = 0 ;
                printf ("\nEnter marks of %d subjects for ROLL
                                 NO %d\n", m, roll_number) ;
                for (j =1; j <= m; j++)
                 {
                     scanf ("%d", &marks) ;
                     total = total + marks ;
                 }
                 printf ("TOTAL MARKS = %d", total) ;
                 if (total >= FIRST)
                      printf (" (FIRST DIVISON)\n\n") ;
                 else if (total <= SECOND)
                      printf (" (second Divison)\n\n") ;
                    else
                      printf (" (***FAIL*** )\n\n") ;
           }
  }


Output:    Enter number of students and subjects
                3   6
                Enter roll number : 8701
                Enter marks of 6 subjects for ROLL NO 8701
                81  75  83  45  61  59
                TOTAL MARKS = 404  ( FIRST DIVISON )
                Enter roll_number : 8702
                Enter marks of 6 subjects for ROLL NO 8702
                51  49  55  47  65  41
                TOTAL MARKS = 308 ( Second Divison )
                Enter roll number : 8704
                Enter marks of 6 subjects for ROLL NO 8704
                40  19  31  47  39  25
                TOTAL MARKS = 201 ( ***FAIL*** )
                  

No comments:

Post a Comment