belongs to collection: Loop programs in Java with an examples
Write a Java program to calculate HCF of Two given numbers using loop
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
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
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..
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