If a number is given as input then how to find out the highest common factor using for loop.
Algorithm
1. Define two variables - A, B
2. Set loop from 1 to max of A, B
3. Check if both are completely divided by same loop number, if yes, store it
4. Display the stored number is HCF
Program
import java.util.Scanner; public class GCDOfTwoNumbers { public static void main(String args[]){ int a, b, i, hcf = 0; Scanner sc = new Scanner(System.in); System.out.println("Enter first number :: "); a = sc.nextInt(); System.out.println("Enter second number :: "); b = sc.nextInt(); for(i = 1; i <= a || i <= b; i++) { if( a%i == 0 && b%i == 0 ) hcf = i; } System.out.println("HCF of given two numbers is ::"+hcf); } }