Page 178 - computer science (868) class 11
P. 178

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
                34                  default: System.out.println("Wrong Choice");

                35              }
                36          }

                37      }

              The output of the preceding program is as follows:
              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







                176176  Touchpad Computer Science-XI
   173   174   175   176   177   178   179   180   181   182   183