C Program to Find LCM of two
Solution
Sure, here is a simple C program to find the Least Common Multiple (LCM) of two numbers:
#include <stdio.h>
int find_lcm(int a, int b)
{
static int multiple = 0;
multiple += b;
if((multiple % a == 0) && (multiple % b == 0))
{
return multiple;
}
else
{
return find_lcm(a, b);
}
}
int main()
{
int num1, num2, lcm;
printf("Enter two numbers:\n");
scanf("%d %d", &num1, &num2);
lcm = find_lcm(num1, num2);
printf("LCM of %d and %d is %d", num1, num2, lcm);
return 0;
}
This program works as follows:
- It includes the standard input/output library.
- It defines a function
find_lcmthat calculates the LCM of two numbers using recursion. - In the
mainfunction, it asks the user to input two numbers. - It calls the
find_lcmfunction with the two numbers as arguments. - It prints the LCM of the two numbers.
- It returns 0 to indicate that the program has run successfully.
Similar Questions
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
wap in java to find hcf and lcm of 2 numbers
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
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
If the LCM and HCF of two numbers are equal, then the numbers must be
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.