Page 358 - ComputerScience_Class_11
P. 358

Dynamic Way: Using InputStreamReader
              This is another technique to insert the values by accepting the values from the user.


                Program 19     Write a Java program to accept numbers into a 3 × 3 array and then calculate the sum of even
                               numbers present in odd positions.
                 1       import java.io.*;

                 2       class SumEvenInOddPositions

                 3       {
                 4           public static void main(String args[]) throws IOException

                 5           {

                 6
                                 InputStreamReader isr  =  new InputStreamReader(System.in);
                 7               BufferedReader br= new BufferedReader(isr);
                 8               int n[ ][ ] = new int[3][3];

                 9               int i, j, s=0;

                10               for(i = 0;i<3; i++)

                11               {
                12                   for(j= 0; j<3; j++)

                13                   {

                14                       System.out.print ("Enter a number: ");

                15                       n[i][j] = Integer.parseInt ( br.readLine() );
                16                   }

                17               }

                18

                19               for( i = 0; i<3; i++)
                20               {

                21                   for(j=0; j<3; j++)

                22                   {     if(n[i][j]%2 == 0 && i%2!= 0 && j%2!=0)

                23                           s = s + n[i][j];
                24                   }

                25               }

                26               System.out.println ("Sum of Numbers: " + s);

                27           }
                28       }




                  356  Touchpad Computer Science (Ver. 3.0)-XI
   353   354   355   356   357   358   359   360   361   362   363