Write a Java program to calculate HCF of Two given numbers using loop

Introduction

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..

import java.util.Scanner;
 
public class JavaLoopExcercise
{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
 
        int dividend, divisor;
        int remainder, hcf = 0;
 
        System.out.print("Enter the first number ");
        dividend = console.nextInt();
 
        System.out.print("Enter the second number ");
        divisor = console.nextInt();        
 
        do
    {
            remainder = dividend % divisor;
 
            if(remainder == 0)
            {
                hcf = divisor;
            }
        else
            {
                dividend = divisor;
                divisor = remainder;
        }
 
        }while(remainder != 0);
 
        System.out.println("HCF: " + hcf);
    }  
}

Result

Write a Java program to calculate HCF of Two given numbers using loop
Write a Java program to calculate HCF of Two given numbers using loop

Leave a Comment