Q:

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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:

Enter the first number 5

Enter the second number 45

HCF: 5

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a Java program to enter the numbers till the... >>
<< Write a Java program to check whether the number i...