Page 208 - Computer science 868 Class 12
P. 208

46              ob.getnum(num);
                47              ob.ishappy();

                48          }
                49      }

              The output of the preceding program is as follows:
              OUTPUT 1
              Enter a number

              68
              Happy No.

              OUTPUT 2
              Enter a number

              12
              Not a Happy No.



                Program 9     Write a program to find the sum of the given series:

                              a. S = 1 - 4 + 9 - 16 + ………… to n terms
                              b. Print the series: 1, 11, 111, 1111, ….. N

                1       import java.util.*;

                2       class series
                3       {

                4           public static void main()
                5           {

                6               Scanner sc= new Scanner(System. in);
                7               int ch,s=0,n,i;

                8               System.out.println("1. S = 1 – 4 + 9 – 16 + ………… to n terms");
                9               System.out.println("2. 1, 11, 111, 1111, … n");

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

                12              switch(ch)
                13              {

                14                  case 1: System.out.println("Enter nth terms ");
                15                      n=sc.nextInt();

                16                      for(i=1;i<=n;i++)
                17                      {





                206206  Touchpad Computer Science-XII
   203   204   205   206   207   208   209   210   211   212   213