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..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. import java.util.Scanner;
  2.  
  3. public class JavaLoopExcercise
  4. {
  5. public static void main(String[] args)
  6. {
  7. Scanner console = new Scanner(System.in);
  8.  
  9. int dividend, divisor;
  10. int remainder, hcf = 0;
  11.  
  12. System.out.print("Enter the first number ");
  13. dividend = console.nextInt();
  14.  
  15. System.out.print("Enter the second number ");
  16. divisor = console.nextInt();
  17.  
  18. do
  19. {
  20. remainder = dividend % divisor;
  21.  
  22. if(remainder == 0)
  23. {
  24. hcf = divisor;
  25. }
  26. else
  27. {
  28. dividend = divisor;
  29. divisor = remainder;
  30. }
  31.  
  32. }while(remainder != 0);
  33.  
  34. System.out.println("HCF: " + hcf);
  35. }
  36. }

Result

java techstudy.org

     





Related Topics




Popular Posts