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


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

Kindle Paperwhite High Resolution Display Tablet | Rs. 9999

  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


Responses




Popular Posts

Facebook

Founded in 2016, Tech Study passionately delivers stylish and dynamic innovative information to programmer and technology lovers. http://www.techstudy.org/.