Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class gcd {
- public int highestcommonfactor(int a, int b) {
- int hcf;
- hcf = 1;
- int c;
- //Renames variables so that a > b
- if (a < b) {
- c = a;
- a = b;
- b = c;
- }
- int i;
- for (i = b; i >=1;i--) {
- //finds the remainder on division by i of a, b.
- int remainderbOveri = b%i;
- int remainderaOveri = a%i;
- //If i divides both b and a, then sets hcf to i and stops the loop.
- if(remainderbOveri == 0 && remainderaOveri == 0){
- hcf = i;
- break;
- }
- }
- return hcf;
- }
- public static void main(String args[]){
- gcd something = new gcd();
- int numerator, denominator;
- numerator = 99; denominator = 44;
- int output = something.highestcommonfactor(numerator,denominator);
- System.out.println(output);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement