Search⌘ K
AI Features

Finding the Highest Common Factor

Explore how to write a Ruby program that calculates the highest common factor of two non-negative integers. Learn to iterate through arrays, use loop control statements like break, and compare elements to identify the greatest common divisor.

We'll cover the following...

Problem

Write a program that asks the user to enter two non-negative integers and find their highest common factor (HCF).

Enter the first number: (8)
Enter the second number: (12)
The HCF of 8 and 12 is: 4
Calculating HCF of two numbers

The largest common factor of two or more numbers is called the highest common factor. The HCF is also known as the greatest common divisor (GCD).

For example,

8=18 = 1 ×\times 8=28 = 2 ×\times ...