Page 226 - ComputerScience_Class_11
P. 226

10              System.out.println("2. s = 1/a + 2/a + 3/a + 4/a +.... + to n terms ");

                11              System.out.println("Enter your choice ");
                12              ch=sc.nextInt();

                13              switch(ch)
                14              {
                15                  case 1: System.out.println("Enter nth term ");

                16                      n=sc.nextInt();
                17                      for(i=1; i<=n; i++)

                18                      {
                19                          s=s+1.0/(double)i;
                20                      }

                21                      System.out.println("Sum = " +s);
                22                      break;

                23                  case 2: System.out.println("Enter nth term ");
                24                      n=sc.nextInt();
                25                      System.out.println("Enter \"a\" ");

                26                      a=sc.nextDouble();
                27                      for(i=1; i<=n; i++)
                28                      {

                29                          s=s+i/a;
                30                      }

                31                      System.out.print(s);
                32                      break;
                33                  default: System.out.println("Wrong Choice");

                34              }
                35          }
                36      }


              The output of the preceding program is as follows:
                     BlueJ: Terminal Window - Java

                 Options

                1. s = 1 + 1/2 + 1/3 + ..... 1/n
                2. s = 1/a + 2/a + 3/a + 4/a +.... + to n terms
                Enter your choice
                1
                Enter nth term
                5
                Sum = 2.283333333333333




                  224  Touchpad Computer Science (Ver. 3.0)-XI
   221   222   223   224   225   226   227   228   229   230   231