Page 324 - ComputerScience_Class_11
P. 324

11.2 NEED FOR AN ARRAY
              When the program requires us to create many variables of the same data type, for example, if we need to take 100
              integer values one after another, then it becomes very difficult to create separate variables and take values. So, we use
              the concept of Array. Let us understand this by taking the following example.


                Program 1      Write a Java program to input marks of five students in English and print the marks, their
                               sum and average.
                 1       import java.util.*;

                 2       class sum_avg
                 3       {
                 4          public static void main(String args[])
                 5          {

                 6              Scanner sc = new Scanner(System.in);
                 7              int a, b, c, d, e, sum;
                 8              double avg;
                 9              System.out.println("Enter marks of five students in English");
                10              a=sc.nextInt();  b=sc.nextInt(); c=sc.nextInt();
                11              d=sc.nextInt();  e=sc.nextInt();

                12              sum = a+b+c+d+e;
                13              avg=sum/5.0;
                14              System.out.println("The entered numbers:");
                15              System.out.println(a+","+b+","+c+","+d+","+e);

                16              System.out.println("The total marks are: " +sum);
                17              System.out.println("The average marks are: " +avg);
                18          }
                19       }

              The output of the preceding program is as follows:

                      BlueJ: Terminal Window - Java
                   Options

                  Enter marks of ten students in English
                  45
                  65
                  98
                  74
                  95
                  The entered numbers:
                  45,65,98,74,95
                  The total marks are: 377
                  The average marks are: 75.4





                  322  Touchpad Computer Science (Ver. 3.0)-XI
   319   320   321   322   323   324   325   326   327   328   329