usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

Finding HCF and GCD in Java - Kids Fun Exercise



What is HCF and GCD? | Maths Explanation for Java Kids

Highest Common Factor or Greatest Common Divisor is the highest-valued-factor or greatest-valued-divisor common to a set of numbers.

In this Java math project, we'll explore how to find the Highest Common Factor (HCF) and Greatest Common Divisor (GCD) of numbers. This tutorial is perfect for primary school students learning to code with Java.

A common method for finding H.C.F. - G.C.D. is repeated factorization using only common factors.

If we have the set of numbers 30, 48 and 54 for example, their H.C.F. or G.C.D. is found thus:

Steps for calculating HCF using factorization method

Hence, H.C.F. of 30, 48 and 54 = 2 X 3 = 6


How to find HCF of multiple numbers in Java | Step-by-step Guide.

We shall follow the steps below in our Java algorithm for finding HCF.

Step 1:

Do a numerical sort on the set so its first member is the smallest in the set.

Step 2:

Starting with 2, iteratively check through the set of numbers for a common factor.

Step 3:

For each common factor, divide every member of the number set by the common factor.

Step 4:

Repeat from step 2 recursively until there are no more common factors.


Create a new Java class file; File, New File.
Call it findHCF.
Type out the adjoining Java code for finding Highest Common Factor (Greatest Common Divisor).(H.C.F.)


Note: You can comment out the Java code for the main class from the previous lesson if you have been following.


So! Java Fun Practice Exercise - Find HCF

As a fun practice exercise, feel free to try out your own numbers, and see how the Java code finds the HCF of those numbers.









<< PreviousNext >>