Page 210 - Computer science 868 Class 12
P. 210

Program 10    Input two numbers and print the hcf and lcm of the number.

                1       import java.util.*;

                2       class hcf_lcm
                3       {

                4          public static void main()
                5          {

                6             int temp1, temp2, num1, num2, temp, hcf, lcm;
                7             Scanner sc = new Scanner(System.in);

                8             System.out.print("Enter two numbers: ");
                9             num1 = sc.nextInt();

                10            num2 = sc.nextInt();
                11            temp1 = num1;

                12            temp2 = num2;
                13            while(temp2 != 0)
                14            {

                15               temp = temp2;

                16               temp2 = temp1%temp2;
                17               temp1 = temp;
                18            }

                19            hcf = temp1;
                20            lcm = (num1*num2)/hcf;

                21            System.out.println("HCF of "+ num1 +" and " + num2 + " : "+hcf);
                22            System.out.println("LCM of "+ num1 +" and " + num2 + " : "+lcm);

                23         }
                24      }


              The output of the preceding program is as follows:
              Enter two numbers: 4
              22
              HCF of 4 and 22 : 2
              LCM of 4 and 22 : 44














                208208  Touchpad Computer Science-XII
   205   206   207   208   209   210   211   212   213   214   215