Page 200 - CA_Blue( J )_Class10
P. 200

Program 1    To input 2 numbers and print the factors of each number taken.


                 1  import java.util.*;
                 2  class nested_for

                 3  {    public static void main()
                 4      {   int i,j,n;

                 5          Scanner sc= new Scanner(System.in);
                 6          for(i=1;i<=2;i++)

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

                 9              System.out.println("Factors of : " + n);
                10              for(j=1;j<=n;j++)

                11              {   if(n%j==0)
                12                  {        System.out.print(j+ " " );           }

                13              }
                14              System.out.println();
                15          }

                16      }

                17  }
              You will get the following output:














              Dry Run of the above program:

                               i              n             j             n % j        Output (Factors of n)
                               1              4             1            4%1==0                 1
                                                            2            4%2==0                 2
                                                            3            4%3==1             No Output
                                                            4            4%4==0                 4
                               2              3             1            3%1==0                 1
                                                            2            3%2==1             No Output
                                                            3            3%3==0                 3







                198198  Touchpad Computer Applications-X
   195   196   197   198   199   200   201   202   203   204   205