August 09, 2012

Program to find LCM and HCF in java

Description : A program in java to find least common multiple and Highest common factor

 import java.util.Scanner;


public class LCM {

    /**
     * Computer Science practical question for exams
     */
    private Scanner scanner_ins = null; // To scan user inputed value
   
    private int totalNumber = 0;         //     totalNumber by user
    private int minimumValue = 0;       //     Minimum value inside the user inputed array
    private int lcm = 1;
    private int hcf = 1;
   
    private int [] value_array = null;  // User entered value
   
   
    private int [] denominators = {1,9,8,7,6,5,4,3,2};
    //private int [] commonMultiples = null;
   
   
    /**
     * Constructor
     */
    LCM(){
        scanner_ins = new Scanner(System.in);
    }
   
   
    private void getUserInputs() {
        // TO get user input  Total elements
        System.out.println(" Enter the total number of elements (Say n) : ");
        totalNumber = scanner_ins.nextInt();   //as like scanf, cin in cpp
      
        value_array = new int[totalNumber];  // initialized the size of array
      
      
        for(int i =0 ;i < totalNumber; i++)   //This for loop used to get the elements from user
        {
            System.out.print(" Enter the "+(i+1)+"th elements : ");
            value_array[i]=scanner_ins.nextInt();
        }
      
    }
   
   
    private void findMinimumValue() {
        // TO Find Minimum value inside value_array[]
      
        int temp =0;   // A temporary variable
        temp = value_array[0];  // assingning first value of array to temp
      
        for(int i=1; i< totalNumber; i++)  // For loop compare the temp values to all other values in the array
        {
            if(temp > value_array[i])
            {
                temp = value_array[i];       // changing the temp value to minimum
            }
          
        }
        minimumValue = temp;
    }
   
   
   

    private void findCommomMultiples() {
        // TODO Auto-generated method stub
        denominators[0] = minimumValue;
        int reminder =0;   // reminder
        boolean isCommonMultiper = false;  // is a common multipler
        for(int denoPstn =0; denoPstn < denominators.length; denoPstn ++)
        {
            System.out.println("deno : "+ denominators[denoPstn]);
            System.out.println();
            for(int valuePstn = 0; valuePstn < totalNumber; valuePstn++)
            {
                reminder = value_array[valuePstn] % denominators[denoPstn];
                System.out.println(" value "+ value_array[valuePstn]);
                if(reminder == 0)
                {
                    isCommonMultiper = true;
                    continue;
                }
                else
                {
                    isCommonMultiper = false;
                    break;
                }
              
            }
            if(isCommonMultiper == true)
            {
                lcm = lcm * denominators[denoPstn];
              
                for(int i = 0; i < totalNumber ; i++)
                {
                    if(value_array[i] !=0)
                    value_array[i] = value_array[i] / denominators[denoPstn];
                    else if(value_array[i]==1)
                        System.out.println(" got 1");
                  
                    System.out.print("  value "+ value_array[i]);
                  
                }
            }
            //System.out.println(" reminder :"+ reminder);
        }
        hcf = lcm;
    }
   
   

    private void findLCM() {
        // TO calculate Lcm
      
        for(int i=0; i < value_array.length; i++)
        {
            lcm = lcm * value_array[i];
        }
      
    }
   
   
   
   
    public static void main(String[] highestCommonfactor) {
        // main class where program starts
        LCM lcm_ins = new LCM();  //instance (ins) of Lcm class
        lcm_ins.getUserInputs();  //for getting user input (total elements, elements into array)
        long startTime = System.currentTimeMillis();
        lcm_ins.findMinimumValue();  //To find minimum value user inputed
        lcm_ins.findCommomMultiples();
        lcm_ins.findLCM();
        lcm_ins.showDetails((System.currentTimeMillis() - startTime));
    }




    private void showDetails(long timeTaken) {
      
        System.out.println("\n\t PRIME FACTORS \n ");

      
        System.out.print("\n\t User inputs   [ ");
        for(int i = 0 ; i < totalNumber ; i++)   //This for loop used to user inputed values
        {
            System.out.print(" " + (value_array[i] * hcf));
        }
        System.out.println(" ]");
      
      
        System.out.print("\n\t After dividing by common multiples   [ ");
        for(int i = 0 ; i < totalNumber ; i++)   //This for loop used to view value array after iteration
        {
            System.out.print(" " + value_array[i]);
        }
        System.out.println(" ]");
      
      
        System.out.println("\t  H C F is "+hcf);
        System.out.println("\t  L C M is "+lcm);
      
        System.out.println("\t Computation Time : "+timeTaken+" ms");
      
    }

   

}
 




The above  program is just a simple one. Check here to know how to find the missing number in a sequence. the number is inserted randomly. I hope the complexity is minimum b-lazy java


Similar posts


Finding HCF and LCM

Finding Factorial using recursion

Fibonacci series

Binary search in java

Palindrome in java

Geometric mean using java 

Prime number in java

Print triangle in java

Find missing number in java


Java Source codes

6 comments:

  1. You have provided a fantastic resource.
    Here is my web site Metal Fabrication Melbourne

    ReplyDelete
  2. Thank you for writing these details on your website.
    My web site ... Metal Brackets

    ReplyDelete
  3. Thank you , it helped me a lot... very easy to understand also...

    Sorry for my english!

    ReplyDelete
  4. I almost never leave remarks, however i did some searching and wound up here
    "Program to find LCM and HCF in java". And I actually
    do have 2 questions for you if you tend not to mind.
    Could it be simply me or does it give the impression like some of the remarks look like coming from brain dead
    folks? :-P And, if you are posting on other places, I'd like to follow everything new you have to post. Would you make a list of every one of your community pages like your Facebook page, twitter feed, or linkedin profile?

    Feel free to surf to my site :: extreme Couponing

    ReplyDelete

Your feedback may help others !!!

Facebook comments