Skip to main content

Write a program to accept an even digit number(i.e. number of digits should even, example: 12,2456,245678,...). Split the number into two halves and display the hcf and lcm of them. Example: Input: 1224 Output: First half: 12 Second half: 24 Hcf: 12 Lcm: 24

 Write a program to accept an even digit number(i.e. number of digits should even, example: 12,2456,245678,...). Split the number into two halves and display the hcf and lcm of them.
Example:
Input:  1224
Output:
First half: 12
Second half: 24
Hcf: 12
Lcm: 24


import java.util.Scanner;
class HCF
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number: ");
        int n = sc.nextInt();
        int m=n,c=0;
        while(m>0)
        {
            c++; //for counting the number of digits
            m=m/10;
        }
        if(c%2==0)
        {
            int sq = (int)Math.pow(10,c/2);
            int f = n / sq; //first part
            int s = n % sq; //second part
            int a=f,b=s;
            while(b>0) // hcf calculation
            {
                int t = b;
                b = a % b;
                a = t;
            }
            int lcm = (f*s)/a;
            System.out.println("HCF of two parts is :"+a);
            System.out.println("LCM of two parts is :"+lcm);
        }
        else
            System.out.println("Not a even-digit number");
    }
}

Popular posts from this blog