Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. public class gcd {
  2.  
  3. public int highestcommonfactor(int a, int b) {
  4. int hcf;
  5. hcf = 1;
  6. int c;
  7. //Renames variables so that a > b
  8. if (a < b) {
  9. c = a;
  10. a = b;
  11. b = c;
  12. }
  13. int i;
  14. for (i = b; i >=1;i--) {
  15. //finds the remainder on division by i of a, b.
  16. int remainderbOveri = b%i;
  17. int remainderaOveri = a%i;
  18. //If i divides both b and a, then sets hcf to i and stops the loop.
  19. if(remainderbOveri == 0 && remainderaOveri == 0){
  20. hcf = i;
  21. break;
  22. }
  23. }
  24. return hcf;
  25. }
  26.  
  27. public static void main(String args[]){
  28. gcd something = new gcd();
  29. int numerator, denominator;
  30. numerator = 99; denominator = 44;
  31. int output = something.highestcommonfactor(numerator,denominator);
  32. System.out.println(output);
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement