Thursday, January 23, 2014

HCF / LCM





HCF and LCM by factorization



import java.io.*;
    public class HCF_LCM
        {
            public static void main()throws IOException
                {
                    int num1,num2;
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("\n\tEnter First No. : ");
                    num1=Integer.parseInt(Br.readLine());
                    System.out.print("\n\tEnter Second No. : ");
                    num2=Integer.parseInt(Br.readLine());  
                    HCF_LCM HL =new HCF_LCM();
                    HL.HCF(Math.max(num1,num2),Math.min(num1,num2));
                    HL.LCM(Math.max(num1,num2),Math.min(num1,num2));
                }
    void HCF(int a, int b)
        {
            int num=0;
            for(int i=1;i<=a;i++)
                {
                    if(a%i==0 && b%i==0)
                        {
                            num=i;
                        }
                }
            System.out.println("\n\tHighest Common Factor of "+a+" and "+b+" is = "+num);
        }
    void LCM(int a, int b)
        {
            int i;
            for(i=a;i<=a*b;i++)
                {
                    if(i%a==0 && i%b==0)
                        {
                            break;
                        }
                }
            System.out.println("\n\tLowest Common Factor of "+a+" and "+b+" is = "+i);
        }       
    }



Output



          Enter First No. : 5
          Enter Second No. : 32

          Highest Common Factor of 32 and 5 is = 1
          Lowest Common Factor of 32 and 5 is = 160



HCF by division Method



import java.io.*;
    public class GCD
        {
            public static void main(String args[])throws IOException
                {
                    int num1,num2;
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("\n\tEnter First No. : ");
                    num1=Integer.parseInt(Br.readLine());
                    System.out.print("\n\tEnter Second No. : ");
                    num2=Integer.parseInt(Br.readLine());  
                    GCD G=new GCD();
                    int result=G.Check(Math.max(num1,num2),Math.min(num1,num2));
                    System.out.println("\n\tGreatest Common Factor of "+num1+" and "+num2+" is = "+result);
                }
    int Check(int a, int b)
        {
           while(b>0)
            {
                int c=a%b;
                a=b;
                b=c;
            }
                return a;

        }

    }



Output


          Enter First No. : 8
          Enter Second No. : 18

          Greatest Common Factor of 8 and 18 is = 2



HCF by Recursive Function



import java.io.*;
    public class GCD_Recursive
        {
            public static void main(String args[])throws IOException
                {
                    int num1,num2;
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("\n\tEnter First No. : ");
                    num1=Integer.parseInt(Br.readLine());
                    System.out.print("\n\tEnter Second No. : ");
                    num2=Integer.parseInt(Br.readLine());  
                    GCD_Recursive G=new GCD_Recursive();
                    int result=G.Check(Math.max(num1,num2),Math.min(num1,num2));
                    System.out.println("\n\tGreatest Common Factor of "+num1+" and "+num2+" is = "+result);
                }
    int Check(int a, int b)
        {
           if(b>0)
            {
                return Check(b,a%b);
            }
           return a;
        }
    }



Output



          Enter First No. : 12
          Enter Second No. : 40

          Greatest Common Factor of 12 and 40 is = 4




No comments: