Given 2 numbers, find their LCM and HCF.Note: Do not use any inbuilt functions/libraries for your main logic.Input FormatFirst line of input contains T - number of test cases. Its followed by T lines, each contains 2 numbers A and B.Constraints1 <= T <= 1051 <= A,B <= 109Output FormatFor each test case, print their LCM and HCF separated by space, separated by newline.Sample Input 044 71013 16 4605904 996510762Sample Output 01420 213 112 27740895599216 78
Question
Given 2 numbers, find their LCM and HCF.Note: Do not use any inbuilt functions/libraries for your main logic.Input FormatFirst line of input contains T - number of test cases. Its followed by T lines, each contains 2 numbers A and B.Constraints1 <= T <= 1051 <= A,B <= 109Output FormatFor each test case, print their LCM and HCF separated by space, separated by newline.Sample Input 044 71013 16 4605904 996510762Sample Output 01420 213 112 27740895599216 78
Solution
To find the LCM and HCF of two numbers, we can follow these steps:
- Read the number of test cases, T.
- For each test case, do the following:
a. Read two numbers, A and B.
b. Initialize variables lcm and hcf as A and B, respectively.
c. Find the LCM (Least Common Multiple) of A and B using the formula:
lcm = (A * B) / HCF(A, B)
d. Find the HCF (Highest Common Factor) of A and B using the Euclidean algorithm:
- Initialize variables x and y as A and B, respectively.
- While y is not equal to 0, do the following:
- Set a temporary variable, temp, as y.
- Set y as the remainder of x divided by y.
- Set x as temp.
- Set hcf as x. e. Print the LCM and HCF separated by a space.
- Repeat step 2 for all test cases.
Here is the code in Python:
def find_lcm_hcf(A, B):
lcm = (A * B) // find_hcf(A, B)
hcf = find_hcf(A, B)
return lcm, hcf
def find_hcf(x, y):
while y:
x, y = y, x % y
return x
T = int(input())
for _ in range(T):
A, B = map(int, input().split())
lcm, hcf = find_lcm_hcf(A, B)
print(lcm, hcf)
This code takes the number of test cases as input, followed by the test cases themselves. It then calculates the LCM and HCF for each test case using the find_lcm_hcf function and prints the results.
Similar Questions
C Program to Find LCM of two
Implement a program that takes two positive integers A and B in the input and prints their LCM.Definition of LCM : The Least Common Multiple or LCM of two numbers say A and B, is denoted as LCM (A,B). And the LCM is the smallest or least positive integer that is divisible by both A and B.Problem Constraint
The product of 2 numbers is 6750 and their LCM is 450. If difference between the numbers is equal to their HCF, then find the smaller number
Single File Programming QuestionProblem StatementSam is on a quest to decipher enchanted scrolls in an ancient temple that requires unveiling the HCF (Greatest Common Divisor) and LCM (Least Common Multiple) of two numbers. Write a program where Sam enters two positive integers, calculates the HCF and LCM using a for loop, and displays them.ExampleInput:6 3Output:HCF = 3LCM = 6Explanation:HCF (Greatest Common Divisor) of two numbers is the largest number that divides both of them without leaving a remainder.For 6 and 3, the common divisors are 1 and 3. The largest of these is 3, so the HCF is 3.LCM (Least Common Multiple) of two numbers is the smallest number that is a multiple of both.To find the LCM, we typically use the formula: LCM(a, b) = (a * b) / HCF(a, b).For 6 and 3, (6 * 3) / 3 = 18 / 3 = 6. So, the LCM is 6.Note: This question helps in clearing AMCAT exam.Input format :The input consists of two space-separated integers: n1 and n2.Output format :The first line prints "HCF = " followed by an integer which is the HCF of n1 and n2.The second line prints "LCM = " followed by an integer which is the LCM of n1 and n2.Refer to the sample outputs for the formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ n1, n2 ≤ 1000Sample test cases :Input 1 :6 3Output 1 :HCF = 3LCM = 6Input 2 :1000 1Output 2 :HCF = 1LCM = 1000
Given 2 numbers - A and B, evaluate AB.Note:Do not use any inbuilt functions / libraries for your main logic.Input FormatThe first line of input contains T - the number of test cases. It's followed by T lines, each line containing 2 numbers - A and B, separated by space.Output FormatFor each test case, print AB, separated by new line. Since the result can be very large, print result % 1000000007Constraints30 points1 <= T <= 10000 <= A <= 1060 <= B <= 10370 points1 <= T <= 10000 <= A <= 1060 <= B <= 109ExampleInput45 21 102 3010 10Output25173741817999999937
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.