Page 210 - CA_Blue( J )_Class9
P. 210

Program 11     Input a number and print the first factor of the number apart from 1.

                     1     import java.util.*;

                     2     class first_factor
                     3     {   public static void main()

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

                     6             System.out.print("Enter a number: ");
                     7             n=sc.nextInt();

                     8             for (i=2; i<=n;i++)
                     9             {

                    10                 if(n%i==0)
                    11                 {   break;    }
                    12             }

                    13     System.out.println("First factor of "+n + " is : "+i);

                    14         }
                    15     }
                  You will get the following output:










                  continue
                  This statement used to control the loop by sending the control to the next iteration. When it is executed, the
                  current iteration stops and skips the remaining code, then it goes to the next iteration. All three types of loops can
                  use Java continue statement.
                  Syntax:

                      for (initialization; condition for testing; increment or decrement)
                      {  if(condition)
                             {                continue;        }
                      Statements inside loop;
                      }
                      1st statement after loop;

                   Program 12     Input 5 numbers and print the product of the even numbers.

                     1     import java.util.*;
                     2     class product_even_number

                     3     {   public static void main()


                   208    Touchpad Computer Applications-IX
   205   206   207   208   209   210   211   212   213   214   215