TCS Coding Practice Question | HCF or GCD of 2 Numbers Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Given two numbers, the task is to find the HCF of two numbers using Command Line Arguments. GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that divides both of them. Examples: Input: n1 = 10, n2 = 20 Output: 10 Input: n1 = 100, n2 = 101 Output: 1 Approach: Since the numbers are entered as Command line Arguments, there is no need for a dedicated input lineExtract the input numbers from the command line argumentThis extracted numbers will be in String type.Convert these numbers into integer type and store it in variables, say num1 and num2Find the HCF of the numbers. An efficient solution is to use Euclidean algorithm which is the main algorithm used for this purpose. The idea is, GCD of two numbers doesn’t change if smaller number is subtracted from a bigger number.Print or return the HCF Program: C // C program to compute the HCF of two numbers // using command line arguments #include <stdio.h> #include <stdlib.h> /* atoi */ // Function to compute the HCF of two numbers int HCF(int a, int b) { if (b == 0) return a; return HCF(b, a % b); } // Driver code int main(int argc, char* argv[]) { int num1, num2; // Check if the length of args array is 1 if (argc == 1) printf("No command line arguments found.\n"); else { // Get the command line argument and // Convert it from string type to integer type // using function "atoi( argument)" num1 = atoi(argv[1]); num2 = atoi(argv[2]); // Find the HCF and print it printf("%d\n", HCF(num1, num2)); } return 0; } Java // Java program to compute the HCF of two numbers // using command line arguments class GFG { // Function to compute the HCF of two numbers static int HCF(int a, int b) { if (b == 0) return a; return HCF(b, a % b); } // Driver code public static void main(String[] args) { // Check if length of args array is // greater than 0 if (args.length > 0) { // Get the command line argument and // Convert it from string type to integer type int num1 = Integer.parseInt(args[0]); int num2 = Integer.parseInt(args[1]); // Find the HCF int res = HCF(num1, num2); // Print the HCF System.out.println(res); } else System.out.println("No command line " + "arguments found."); } } Output: In C: In Java: Time Complexity: O(log(min(num1,num2)))Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article GCF Calculator R RishabhPrabhu Follow Improve Article Tags : Interview Experiences TCS TCS-coding-questions Practice Tags : TCS Similar Reads TCS Coding Practice Question | LCM of 2 Numbers Given two numbers, the task is to find the LCM of two numbers using Command Line Arguments. LCM (Least Common Multiple) of two numbers is the smallest number which can be divided by both numbers. For example, LCM of 15 and 20 is 60 and LCM of 5 and 7 is 35. Examples: Input: n1 = 10, n2 = 15 Output: 3 min read GCF Calculator GCF or GCD or HCF Calculator is a tool designed by experts at GeeksforGeeks to simplify the lives of those who need to calculate the GCD of numbers. With this calculator, you can input numbers separated by commas, and the calculator will provide their GCD.GCD of a set of numbers is the greatest poss 3 min read Java Program to Find GCD or HCF of Two Numbers GCD (i.e. Greatest Common Divisor) or HCF (i.e. Highest Common Factor) is the largest number that can divide both the given numbers. Example: HCF of 10 and 20 is 10, and HCF of 9 and 21 is 3.Therefore, firstly find all the prime factors of both the stated numbers, then find the intersection of all t 2 min read HCF Questions Highest Common Factor (HCF) or Greatest common divisor (GCD) is a simple but important math concept. It's like finding the biggest number that evenly divides two or more numbers. Knowing how to do this can be handy in solving various math problems.The HCF or GCD of two numbers is defined as the larg 9 min read GCD (Greatest Common Divisor) Practice Problems for Competitive Programming GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest positive integer that divides both of the numbers.GCD of Two NumbersFastest Way to Compute GCDThe fastest way to find the Greatest Common Divisor (GCD) of two numbers is by using the Euclidean algorithm. The E 4 min read PHP Program to Find GCD or HCF of Two Numbers Given two numbers, the task is to find the GCD or HCF of two numbers in PHP. GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest positive integer that divides both numbers without leaving a remainder. Formula to Find GCDGCD ( a, b ) = [ |a.b| ] / [ lcm(a, b) ]H 2 min read Like