Page 206 - CA_Blue( J )_Class9
P. 206

Write a program to input a number and print the sum of the following series.
                   Program 7
                                  s= 1/2 + 2/3 + 3/4 + 4/5 + 5/6 – using do-while
                     1     class series

                     2         {
                     3             public static void main()

                     4             {
                     5                 float s=0.0f, i;

                     6                 i=1;
                     7                 do

                     8                 {
                     9                     s = s + i/(i+1);

                    10                     i=i+1;
                    11                 }
                    12                 while(i<=5);

                    13                 System.out.println("Result : "+s);

                    14             }
                    15         }
                  You will get the following output:











                  9.3.5 Interconversion between different types of Loops
                  Code always allows flexibility in choosing the type of loop. We select the loops according to the ease of logic of the
                  code. We can also convert any type of loop to another loop just by changing the place of the parts of the loops.

                                     for (initialization; condition for testing; increment or
                                        decrement)
                                     {  // job performed by the body of the loop;              }



                                     initialization;
                                     while (condition for testing)
                                     {  // job performed by the body of the loop;
                                     increment or decrement;
                                     }


                                     initialization;
                                     do
                                     {  // job performed by the body of the loop;
                                     increment or decrement;
                                     } while (condition for testing);


                   204    Touchpad Computer Applications-IX
   201   202   203   204   205   206   207   208   209   210   211