Write a java program to find out HCF (Highest Common Factor) of numbers using for loop Eg num1=12 and num2=18 so HCF=6
anyone need this id⭐
Answers
Answer:
public class HCF Example1 {
public static void main(String[] args) {
//Lets take two numbers 12 and 18 and find their HCF
int num1 = 12, num2 = 18, HCF = 6;
/* loop is running from 1 to the smallest of both numbers
* In this example the loop will run from 1 to 12 because 12
* is the smaller number. All the numbers from 1 to 55 will be
* checked. A number that perfectly divides both numbers would
* be stored in variable "HCF". By doing this, at the end, the
* variable HCF will have the largest number that divides both
* numbers without remainder.
*/
for(int i = 1; i <= num1 && i <= num2; i++)
{
if(num1%i==0 && num2%i==0)
HCF = i;
}
System.out.printf("HCF of %d and %d is: %d", num1, num2, HCF);
}
}