Page 225 - ComputerScience_Class_11
P. 225

31                      for(i=1; i<=n; i++)

                  32                      {

                  33                          s=s*10+1;
                  34                          System.out.print(s+ " , ");
                  35                      }

                  36                      break;
                  37                  default: System.out.println("Wrong Choice");

                  38              }
                  39          }

                  40      }

                 The output of the preceding program is as follows:

                       BlueJ: Terminal Window - Java

                   Options

                  a. s = 1 - 4 + 9 - 16 + ………… to n terms
                  b. 1, 11, 111, 1111, ……..… to n terms
                  Enter your choice
                  a
                           th
                  Enter n  term
                  5
                  Sum = 15




                  Program 11     Write a program in Java to find the sum of the given series after accepting the value of ‘n’
                                 from the user.

                                                 1
                                                             1
                                                     1
                                        1.  s = 1 +   +   + ….. +
                                                 2   3       n
                                              1  2   3   4
                                        2.  s =   +   +   +   + …. + to n terms
                                              a  a   a   a
                   1      import java.util.*;
                   2      class series2
                   3      {
                   4          public static void main(String args[])

                   5          {
                   6              Scanner sc= new Scanner(System.in);

                   7              int ch, n, i;
                   8              double s=0, a;
                   9              System.out.println("1. s = 1 + 1/2 + 1/3 + ..... 1/n");






                                                                                              Statements and Scope  223
   220   221   222   223   224   225   226   227   228   229   230