HCF Of two Number

Programming codes

Grab The Best Job for you

example, category, and, terms

Share now

Definition & Explanation

The Highest Common Factor (HCF), also known as the Greatest Common Divisor (GCD), of two or more numbers is the largest positive integer that divides each of the numbers without leaving a remainder. To find the HCF of two numbers, various methods can be used, such as prime factorization, the Euclidean Algorithm, or brute-force checking of common factors. The HCF is crucial in simplifying fractions, finding equivalent fractions, and solving certain mathematical problems.

Example: Consider finding the HCF of the numbers 36 and 60.

Explanation of the Euclidean Algorithm:

  1. Start with the two numbers: 36 and 60.
  2. Divide the larger number (60) by the smaller number (36): 60 ÷ 36 = 1 remainder 24.
  3. Replace the larger number (60) with the smaller number (36) and the smaller number (36) with the remainder (24): new numbers are 36 and 24.
  4. Repeat the division: 36 ÷ 24 = 1 remainder 12.
  5. Again, replace the larger number (36) with the smaller number (24) and the smaller number (24) with the remainder (12): new numbers are 24 and 12.
  6. Repeat the division: 24 ÷ 12 = 2 remainder 0.
  7. The remainder becomes 0, indicating that 12 is the HCF of 36 and 60.

Therefore, the HCF of 36 and 60 is 12.

This method efficiently finds the HCF by repeatedly dividing the larger number by the smaller number and replacing the numbers until the remainder becomes zero, giving the HCF.

Program to find the HCF of Number

  1. Initialize num1 with the value 36 and num2 with the value 60.
  2. Initialize hcf with the value 1.
  3. Start a loop from i equals 1 up to the minimum of num1 and num2, which is 36.
  4. Check if both num1 and num2 are divisible by the current value of i.
  5. If i is a divisor of both num1 and num2, update hcf to the value of i.
  6. Repeat steps 4-5 until i reaches the minimum of num1 and num2.
  7. After the loop, hcf will contain the highest common factor of num1 and num2, which is 12 in this case.
  8. Print the value of hcf, which is 12.

This approach checks all numbers from 1 up to the minimum of num1 and num2 to find their highest common factor. While it’s simple, it may not be efficient for large numbers compared to other methods like prime factorization or the Euclidean Algorithm.

C

Method 1 :

#include <stdio.h>

int main() {
    int num1 = 36;
    int num2 = 60;
    int hcf = 1;

    for (int i = 1; i <= (num1 < num2 ? num1 : num2); i++) {
        if (num1 % i == 0 && num2 % i == 0) {
            hcf = i;
        }
    }

    printf("HCF OF Number %d and %d is %d\n", num1, num2, hcf);

    return 0;
}

Output :

HCF OF Number 36 and 60 is 12
C++HCF OF Number 36 and 60 is 12

Method 1 :

#include <iostream>
using namespace std;

int main() {
    int num1 = 36;
    int num2 = 60;
    int hcf = 1;

    for (int i = 1; i <= min(num1, num2); i++) {
        if (num1 % i == 0 && num2 % i == 0) {
            hcf = i;
        }
    }

    cout << "HCF OF Number " << num1 << " and " << num2 << " is " << hcf << endl;

    return 0;
}

Output :

HCF OF Number 36 and 60 is 12
JAVA

Method 1 :

public class Main {
    public static void main(String[] args) {
        int num1 = 36;
        int num2 = 60;
        int hcf = 1;

        for (int i = 1; i <= Math.min(num1, num2); i++) {
            if (num1 % i == 0 && num2 % i == 0) {
                hcf = i;
            }
        }

        System.out.println("HCF OF Number " + num1 + " and " + num2 + " is " + hcf);
    }
}

Output :

HCF OF Number 36 and 60 is 12
Python

Method 1 :

num1=36
num2=60
hcf=1

for i in range(1,min(num1,num2)):
    if num1%i==0 and num2%i==0:
        hcf=i

print("HCF OF Number",num1,"and",num2,"is",hcf)

Output :

HCF OF Number 36 and 60 is 12

Leave a Reply

Your email address will not be published. Required fields are marked *