Page 207 - CA_Blue( J )_Class9
P. 207

Program 8      Input a number and print the factors of the number using for loop.

                   1      import java.util.*;

                   2      class for_factors
                   3      {

                   4          public static void main()
                   5          {

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

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

                  10              System.out.print("The factors of "+n +" are : ");
                  11              for(i=1;i<=n;i++)
                  12              {

                  13                  if(n%i==0)

                  14                  {   System.out.print(i+" ");
                  15                  }
                  16              }

                  17          }
                  18      }

                 You will get the following output:












                  Program 9      Input a number and print the factors of the number using while loop.

                   1      import java.util.*;
                   2      class while_factors

                   3      {
                   4          public static void main()

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

                   7              int i,n;
                   8              System.out.print("Enter a number : ");



                                                                                       Iterative Constructs in Java  205
   202   203   204   205   206   207   208   209   210   211   212