Highest Common Factor

Definition & Explanation

Example:
Input:
Output:

Heading

Desc

C

Method 1 :

#include <stdio.h>

int main() {
    int a = 36, b = 60, hcf = 1;
    for (int i = 1; i <= (a < b ? a : b); i++) {
        if (a % i == 0 && b % i == 0) {
            hcf = i;
        }
    }
    printf("HCF of %d and %d is %d\n", a, b, hcf);
    return 0;
}

Output :

C++

Method 1 :

#include <iostream>
using namespace std;

int main() {
    int a = 36, b = 60, hcf = 1;
    for (int i = 1; i <= (a < b ? a : b); i++) {
        if (a % i == 0 && b % i == 0) {
            hcf = i;
        }
    }
    cout << "HCF of " << a << " and " << b << " is " << hcf << endl;
    return 0;
}

Output :

JAVA

Method 1 :

public class Main {
    public static void main(String[] args) {
        int a = 36, b = 60, hcf = 1;
        for (int i = 1; i <= (a < b ? a : b); i++) {
            if (a % i == 0 && b % i == 0) {
                hcf = i;
            }
        }
        System.out.println("HCF of " + a + " and " + b + " is " + hcf);
    }
}

Output :

Python

Method 1 :

a=36
b=60
hcf=1
for i in range(1,min(a,b)):
    if a%i==0 and b%i==0:
        hcf=i
print("hcf of",a,"and",b, "is",hcf)

Output :

hcf of 36 and 60 is 12