Page 211 - CA_Blue( J )_Class9
P. 211

4          {   Scanner sc= new Scanner(System.in);
                   5              int n,i=1,p=1;

                   6              while(i<=5)
                   7              {

                   8                  System.out.print("Enter a number : ");
                   9                  n=sc.nextInt();
                  10                  if(n%2==1)

                  11                  {   i++; continue;    }

                  12                  p=p*n;
                  13                  i++;
                  14              }

                  15              System.out.println("Product of even numbers "+ p);}
                  16      }

                 You will get the following output:













                 return
                 In Java, when a method is called, the control is transferred from the caller module to the method which has
                 to return back to the caller module after the execution of the method. This is done by the return statement.
                 If the method does not return any value, we have to use the keyword "void" else the datatype of the returned
                 value must be specified.

                 Syntax:
                    return_data_type function_name(arguments)
                    {
                        Job;
                        return statement;
                    }


                                 Write a program to print the square of a number taken as parameter using a function int
                  Program 13
                                 square(int).
                   1      class function
                   2      {

                   3          int square(int n)
                   4          {




                                                                                       Iterative Constructs in Java  209
   206   207   208   209   210   211   212   213   214   215   216