Take Two Integers And Find Their HCF | Java Programs

Output:

*** HCF Of Two Numbers ***

Enter the first number: 3
Enter the second number: 4
HCF: 1

Click Here For Java Online Compiler

Solution:

import java.util.Scanner;

class HCF
{
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
     
        int dividend, divisor;
        int remainder, hcf = 0;
        System.out.println("*** HCF Of Two Numbers ***" + "\n");
        System.out.print("Enter the first number: ");
        dividend = scanner.nextInt();
        
        System.out.print("Enter the second number: ");
        divisor = scanner.nextInt();        
        
        do
        {
            remainder = dividend % divisor;
            
            if(remainder == 0)
            {
                hcf = divisor;
            } else {
                dividend = divisor;
                divisor = remainder;
            }

        } while (remainder != 0);

        System.out.println("HCF: " + hcf);
    }
}
Share This :

Related Post

avatar

Nice article for find hcf for two number . you can visit one more way to find HCF and LCM of two number

delete 11 April 2020 at 02:58



sentiment_satisfied Emoticon